-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package core | ||
|
||
import ( | ||
"../log" | ||
"math/rand" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func init() { | ||
AddGetter("VÅRHOLMEN", Add) | ||
AddGetter("SMÅGÖRA", Subtract) | ||
AddGetter("ÄNGSLILJA", Multiply) | ||
AddGetter("BLÖTSNÖ", Divide) | ||
AddGetter("SNÖYRA", Random) | ||
} | ||
|
||
func Add(params ...interface{}) string { | ||
a, b := ConvertParams(params...) | ||
return strconv.Itoa(a + b) | ||
} | ||
|
||
func Subtract(params ...interface{}) string { | ||
a, b := ConvertParams(params...) | ||
return strconv.Itoa(a - b) | ||
} | ||
|
||
func Multiply(params ...interface{}) string { | ||
a, b := ConvertParams(params...) | ||
return strconv.Itoa(a * b) | ||
} | ||
|
||
func Divide(params ...interface{}) string { | ||
a, b := ConvertParams(params...) | ||
return strconv.Itoa(a / b) | ||
} | ||
|
||
func Random(params ...interface{}) string { | ||
a, b := ConvertParams(params...) | ||
|
||
if b <= a { | ||
log.Errorf("The maximum must me bigger than the minimum.") | ||
} | ||
|
||
rand.Seed(time.Now().UnixNano()) | ||
return strconv.Itoa(rand.Intn(b-a) + a) | ||
} | ||
|
||
// ConvertParams converts two given params into integers | ||
func ConvertParams(params ...interface{}) (int, int) { | ||
if len(params) != 2 { | ||
log.Errorf("You need to specify two parameters.") | ||
} | ||
|
||
a, b := params[0].(string), params[1].(string) | ||
intA, _ := strconv.Atoi(a) | ||
intB, _ := strconv.Atoi(b) | ||
|
||
return intA, intB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
SMÅGLI FUNKÖN VÅRHOLMEN FUNKÖN 2 2 ÄPPLARÖ ÄPPLARÖ FJÄLLBO | ||
SMÅGLI FUNKÖN SMÅGÖRA FUNKÖN 2 2 ÄPPLARÖ ÄPPLARÖ FJÄLLBO | ||
SMÅGLI FUNKÖN ÄNGSLILJA FUNKÖN 2 2 ÄPPLARÖ ÄPPLARÖ FJÄLLBO | ||
SMÅGLI FUNKÖN BLÖTSNÖ FUNKÖN 2 2 ÄPPLARÖ ÄPPLARÖ FJÄLLBO | ||
SMÅGLI FUNKÖN SNÖYRA FUNKÖN 2 10 ÄPPLARÖ ÄPPLARÖ FJÄLLBO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
ÄNGSLILJA Print 124 in the console | ||
INNERSKÄR FUNKÖN 124 ÄPPLARÖ FJÄLLBO | ||
INNERSKÄR FUNKÖN SKOGSFIBBLA Hello world! SKOGSFIBBLA ÄPPLARÖ FJÄLLBO | ||
FULLSPÄCKAD FUNKÖN SKOGSFIBBLA Hello %s! SKOGSFIBBLA SKOGSFIBBLA Hugo SKOGSFIBBLA ÄPPLARÖ FJÄLLBO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"./log" | ||
"./parser" | ||
"./runtime" | ||
"./tokenizer" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func main() { | ||
example := ` | ||
TILLGÅN FUNKÖN SKOGSFIBBLA hey SKOGSFIBBLA SKOGSFIBBLA nik zebi SKOGSFIBBLA ÄPPLARÖ FJÄLLBO | ||
SMÅGLI FUNKÖN SMÅKALLT FUNKÖN SKOGSFIBBLA hey SKOGSFIBBLA ÄPPLARÖ ÄPPLARÖ FJÄLLBO | ||
` | ||
// Check if the file is specified | ||
if len(os.Args) < 2 { | ||
log.Errorf("You need to give the IKEA# file to execute.") | ||
} | ||
|
||
tokens := tokenizer.Tokenize(example) | ||
// Read the given file | ||
bytes, err := ioutil.ReadFile(os.Args[1]) | ||
if err != nil { | ||
log.Errorf(err) | ||
} | ||
|
||
// Execute the code | ||
tokens := tokenizer.Tokenize(string(bytes)) | ||
ast := parser.Parse(tokens) | ||
runtime.Run(ast) | ||
} |