From 37b9da32d3b911091deb254f6cba2a137c471287 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 12 Oct 2023 10:51:58 +0200 Subject: move to a direct byte code compiler (#8) * chore: refactor to keep only the new parser and bytecode vm * scanner: remove Token.value field * scanner: remove scanner.kind field * chore: move language specification in lang package This avoid a cyclic dependency in scanner_test which can now use the golang/GoSpec language specification for Go. * clean code * scanner: export scanner fields Also parser now generate function calls, including externals. * chore: fix lint issues * parser: handle strings * wip * parser: implement support for 'if, else, else if' statements Resolving labels in the compiler still in progress. * parser: support if statements, improve compiler * improve handling of functions * improve support of local variables * scanner: trim leading and trailing spaces * fixes to make fibonacci work * parser: improve README, fix function parameters parsing --- _samples/add | 2 ++ _samples/fib | 6 ++++++ _samples/p00 | 1 + _samples/p01 | 2 ++ _samples/p02 | 7 +++++++ _samples/p03 | 3 +++ _samples/p04 | 4 ++++ _samples/p05 | 10 ++++++++++ _samples/p06 | 7 +++++++ _samples/p07 | 5 +++++ 10 files changed, 47 insertions(+) create mode 100644 _samples/add create mode 100644 _samples/fib create mode 100644 _samples/p00 create mode 100644 _samples/p01 create mode 100644 _samples/p02 create mode 100644 _samples/p03 create mode 100644 _samples/p04 create mode 100644 _samples/p05 create mode 100644 _samples/p06 create mode 100644 _samples/p07 (limited to '_samples') diff --git a/_samples/add b/_samples/add new file mode 100644 index 0000000..a403485 --- /dev/null +++ b/_samples/add @@ -0,0 +1,2 @@ +func add(a int, b int) int { return a + b } +add(4, 3) diff --git a/_samples/fib b/_samples/fib new file mode 100644 index 0000000..654c5c0 --- /dev/null +++ b/_samples/fib @@ -0,0 +1,6 @@ +func fib(i int) int { + if i < 2 { return i } + return fib(i-2) + fib(i-1) +} + +println(fib(6)) diff --git a/_samples/p00 b/_samples/p00 new file mode 100644 index 0000000..19f5084 --- /dev/null +++ b/_samples/p00 @@ -0,0 +1 @@ +1+2 diff --git a/_samples/p01 b/_samples/p01 new file mode 100644 index 0000000..baafaa9 --- /dev/null +++ b/_samples/p01 @@ -0,0 +1,2 @@ +s := "Hello" +println(s, 5+2) diff --git a/_samples/p02 b/_samples/p02 new file mode 100644 index 0000000..1aeb4a9 --- /dev/null +++ b/_samples/p02 @@ -0,0 +1,7 @@ +a := 1 + +if a < 2 { + println("yep") +} + +println("ok") diff --git a/_samples/p03 b/_samples/p03 new file mode 100644 index 0000000..39a3cda --- /dev/null +++ b/_samples/p03 @@ -0,0 +1,3 @@ +func f(a int, b int) int { return a + b } + +println("f:", f(3, 4), f(5, 6)) diff --git a/_samples/p04 b/_samples/p04 new file mode 100644 index 0000000..f2a85c8 --- /dev/null +++ b/_samples/p04 @@ -0,0 +1,4 @@ +func f() int { println(a) } + +a := 4 +f() diff --git a/_samples/p05 b/_samples/p05 new file mode 100644 index 0000000..51c2c9b --- /dev/null +++ b/_samples/p05 @@ -0,0 +1,10 @@ +func f(i int) int { + if i < 2 { + if i == 1 { + return + } } + println("i", i) +} + +f(1) +println("bye") diff --git a/_samples/p06 b/_samples/p06 new file mode 100644 index 0000000..88029cc --- /dev/null +++ b/_samples/p06 @@ -0,0 +1,7 @@ +func f(i int) { + if i < 2 { return } + println("i > 1:", i) +} + +f(1) +println("bye") diff --git a/_samples/p07 b/_samples/p07 new file mode 100644 index 0000000..2fa7ee0 --- /dev/null +++ b/_samples/p07 @@ -0,0 +1,5 @@ +func f1() { println("in f1") } + +func f2() { println("in f2"); f1() } + +f2() -- cgit v1.2.3