Skip to content

Commit

Permalink
Create the math core module
Browse files Browse the repository at this point in the history
  • Loading branch information
hugolgst committed Apr 20, 2020
1 parent 6eabcdb commit 52abfa4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
60 changes: 60 additions & 0 deletions core/math.go
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
}
5 changes: 5 additions & 0 deletions examples/math.ikea
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
4 changes: 1 addition & 3 deletions examples/print.ikea
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
20 changes: 15 additions & 5 deletions main.go
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)
}

0 comments on commit 52abfa4

Please sign in to comment.