Compare commits
No commits in common. "2c354c4a967e64309a5719b4a3785eba2daa8b6a" and "10f55313b097c924de6c31e9ee2c3b80e6597c50" have entirely different histories.
2c354c4a96
...
10f55313b0
30 changed files with 967 additions and 1956 deletions
|
|
@ -2,4 +2,4 @@ module neemek.com/anglais/cli
|
|||
|
||||
go 1.25
|
||||
|
||||
require github.com/alecthomas/kong v1.15.0
|
||||
require github.com/alecthomas/kong v1.12.1
|
||||
|
|
|
|||
|
|
@ -4,10 +4,7 @@ github.com/alecthomas/kong v1.5.1 h1:9quB93P2aNGXf5C1kWNei85vjBgITNJQA4dSwJQGCOY
|
|||
github.com/alecthomas/kong v1.5.1/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU=
|
||||
github.com/alecthomas/kong v1.12.1 h1:iq6aMJDcFYP9uFrLdsiZQ2ZMmcshduyGv4Pek0MQPW0=
|
||||
github.com/alecthomas/kong v1.12.1/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU=
|
||||
github.com/alecthomas/kong v1.15.0 h1:BVJstKbpO73zKpmIu+m/aLRrNmWwxXPIGTNin9VmLVI=
|
||||
github.com/alecthomas/kong v1.15.0/go.mod h1:wrlbXem1CWqUV5Vbmss5ISYhsVPkBb1Yo7YKJghju2I=
|
||||
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||
github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs=
|
||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||
|
|
|
|||
|
|
@ -3,14 +3,13 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"github.com/alecthomas/kong"
|
||||
"log"
|
||||
"neemek.com/anglais/core"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/kong"
|
||||
"neemek.com/anglais/core"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
|
|
@ -307,8 +306,6 @@ func (cmd *ReplCmd) Run(ctx *Context) error {
|
|||
vm.SetChunk(c.Chunk)
|
||||
for vm.Next() {
|
||||
}
|
||||
|
||||
println(vm.Stack.Pop().DebugString())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
120
core/all_test.go
120
core/all_test.go
|
|
@ -1,73 +1,113 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type AllTestCase struct {
|
||||
src string
|
||||
expectedStack []Value
|
||||
expectedScope []map[string]Value
|
||||
}
|
||||
|
||||
func GetAllTestCases() map[string]AllTestCase {
|
||||
return map[string]AllTestCase{
|
||||
"constant_number": {
|
||||
"a := 1",
|
||||
[]Value{&IntegerValue{new(big.Int).SetInt64(1)}},
|
||||
[]map[string]Value{
|
||||
{"a": &IntegerValue{new(big.Int).SetInt64(1)}},
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&NumberValue{1},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
"func": {
|
||||
"fn sum(a: int, b: int) -> int {\n\treturn a + b\n}\nres := sum(1, 2)",
|
||||
[]Value{&IntegerValue{new(big.Int).SetInt64(3)}},
|
||||
[]map[string]Value{},
|
||||
"func sum(a: number, b: number) number {\n\treturn a + b\n}\n_ = sum(1, 2)",
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"sum",
|
||||
&FunctionValue{
|
||||
Name: "sum",
|
||||
Params: []FunctionParameter{
|
||||
{
|
||||
"a",
|
||||
&NumberSignature{},
|
||||
},
|
||||
{
|
||||
"b",
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
Chunk: &Chunk{
|
||||
Bytecode: []Bytecode{
|
||||
InstructionDescend,
|
||||
InstructionGetLocal, 0,
|
||||
InstructionGetLocal, 1,
|
||||
InstructionAdd,
|
||||
InstructionReturn,
|
||||
InstructionAscend,
|
||||
},
|
||||
Constants: []Value{&StringValue{"a"}, &StringValue{"b"}},
|
||||
},
|
||||
},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
"list": {
|
||||
"a := [1.0, 2.0]\n{}",
|
||||
[]Value{&NilValue{}},
|
||||
[]map[string]Value{
|
||||
{"a": &ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
"a := [1, 2]",
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&ListValue{
|
||||
[]Value{
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
},
|
||||
},
|
||||
}},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
"constant_list_concat": {
|
||||
"a := [1, 2] + [3]\n{}",
|
||||
[]Value{&NilValue{}},
|
||||
[]map[string]Value{
|
||||
{"a": &ListValue{
|
||||
[]Value{
|
||||
&IntegerValue{big.NewInt(1)},
|
||||
&IntegerValue{big.NewInt(2)},
|
||||
&IntegerValue{big.NewInt(3)},
|
||||
"a := [1, 2] + [3]",
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&ListValue{
|
||||
[]Value{
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
&NumberValue{3},
|
||||
},
|
||||
},
|
||||
}},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
"list_concat": {
|
||||
"a := [1.0, 2.0]\nb := a + [3.0]\nnil",
|
||||
[]Value{&NilValue{}},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &ListValue{
|
||||
"a := [1, 2]\nb := a + [3]",
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
},
|
||||
},
|
||||
"b": &ListValue{
|
||||
0,
|
||||
},
|
||||
&VariableValue{
|
||||
"b",
|
||||
&ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&FloatValue{3},
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
&NumberValue{3},
|
||||
},
|
||||
},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -119,13 +159,7 @@ func TestAll(t *testing.T) {
|
|||
}
|
||||
|
||||
t.Log("Comparing stacks")
|
||||
|
||||
CompareStacks(t, tc.expectedStack, vm.Stack)
|
||||
|
||||
// expected scope == nil => we don't care
|
||||
if tc.expectedScope != nil {
|
||||
CompareScope(t, tc.expectedScope, vm.scope)
|
||||
}
|
||||
CompareStacks(t, tc.expectedStack, vm.stack)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
370
core/compiler.go
370
core/compiler.go
|
|
@ -2,7 +2,6 @@ package core
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -18,9 +17,6 @@ type Compiler struct {
|
|||
source []rune
|
||||
Warnings []CompilerError
|
||||
|
||||
// optimize Whether to attempt some optimization of the emitted bytecode
|
||||
optimize bool
|
||||
|
||||
stack *Stack[LocalVariable]
|
||||
}
|
||||
|
||||
|
|
@ -132,7 +128,6 @@ func NewCompiler(source []rune) *Compiler {
|
|||
nil,
|
||||
source,
|
||||
[]CompilerError{},
|
||||
false,
|
||||
NewStack[LocalVariable](256),
|
||||
}
|
||||
|
||||
|
|
@ -173,14 +168,10 @@ func (c *Compiler) Compile(p *Program) error {
|
|||
}
|
||||
}
|
||||
|
||||
for i, s := range p.Block.statements {
|
||||
for _, s := range p.Block.statements {
|
||||
if err := c.compile(s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if i != len(p.Block.statements)-1 {
|
||||
c.add(InstructionPop)
|
||||
}
|
||||
}
|
||||
|
||||
c.fileStack.Pop()
|
||||
|
|
@ -200,20 +191,16 @@ func (c *Compiler) compile(tree Node) error {
|
|||
tree.(*StringNode).value,
|
||||
})
|
||||
|
||||
case FloatNodeType:
|
||||
case NumberNodeType:
|
||||
c.add(InstructionConstant)
|
||||
c.addConstant(&FloatValue{tree.(*FloatNode).value})
|
||||
|
||||
case IntegerNodeType:
|
||||
c.add(InstructionConstant)
|
||||
c.addConstant(&IntegerValue{tree.(*IntegerNode).value})
|
||||
c.addConstant(&NumberValue{tree.(*NumberNode).value})
|
||||
|
||||
case ListNodeType:
|
||||
l := tree.(*ListNode)
|
||||
|
||||
if len(l.items) == 0 {
|
||||
c.add(InstructionNewList)
|
||||
} else if c.optimize && c.isTreeConstant(l) {
|
||||
} else if c.isTreeConstant(l) {
|
||||
v, err := c.compute(l)
|
||||
if err != nil {
|
||||
panic(err) // this shouldn't happen
|
||||
|
|
@ -242,7 +229,7 @@ func (c *Compiler) compile(tree Node) error {
|
|||
}
|
||||
|
||||
case UnaryNodeType:
|
||||
if c.optimize && c.isTreeConstant(tree.(*UnaryNode).value) {
|
||||
if c.isTreeConstant(tree.(*UnaryNode).value) {
|
||||
v, err := c.compute(tree)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -256,19 +243,9 @@ func (c *Compiler) compile(tree Node) error {
|
|||
return err
|
||||
}
|
||||
|
||||
vt, err := c.deduceSignature(tree.(*UnaryNode).value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch tree.(*UnaryNode).UnaryOperation {
|
||||
case UnaryNegate:
|
||||
if vt.Type() == TypeInteger {
|
||||
c.add(InstructionNegateInt)
|
||||
} else {
|
||||
c.add(InstructionNegateFloat)
|
||||
}
|
||||
|
||||
c.add(InstructionNegate)
|
||||
case UnaryNot:
|
||||
c.add(InstructionNot)
|
||||
}
|
||||
|
|
@ -285,21 +262,12 @@ func (c *Compiler) compile(tree Node) error {
|
|||
c.add(InstructionNil)
|
||||
|
||||
case BlockNodeType:
|
||||
if len(tree.(*BlockNode).statements) == 0 {
|
||||
c.add(InstructionNil)
|
||||
return nil
|
||||
}
|
||||
|
||||
c.addDescend()
|
||||
for i, n := range tree.(*BlockNode).statements {
|
||||
for _, n := range tree.(*BlockNode).statements {
|
||||
err := c.compile(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if i != len(tree.(*BlockNode).statements)-1 {
|
||||
c.add(InstructionPop)
|
||||
}
|
||||
}
|
||||
c.addAscend()
|
||||
|
||||
|
|
@ -315,7 +283,7 @@ func (c *Compiler) compile(tree Node) error {
|
|||
return c.error(fmt.Sprintf("conditional requires boolean; cannot use non-boolean type %s", sig), n.condition)
|
||||
}
|
||||
|
||||
if c.optimize && c.isTreeConstant(n.condition) {
|
||||
if c.isTreeConstant(n.condition) {
|
||||
v, err := c.compute(n.condition)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -353,10 +321,13 @@ func (c *Compiler) compile(tree Node) error {
|
|||
}
|
||||
|
||||
// we store the position of the jump over the else code here
|
||||
// this would jump over the else/otherwise block in the code
|
||||
c.add(InstructionJump)
|
||||
jumpOverElse := c.ip
|
||||
c.advance(2)
|
||||
var jumpOverElse Pos
|
||||
if n.otherwise != nil {
|
||||
// this would jump over the else/otherwise block in the code
|
||||
c.add(InstructionJump)
|
||||
jumpOverElse = c.ip
|
||||
c.advance(2)
|
||||
}
|
||||
|
||||
// put the u16 of where to jump if the condition was false
|
||||
c.putU16(jumpByPos, uint16(c.ip-jumpByPos-2))
|
||||
|
|
@ -366,12 +337,9 @@ func (c *Compiler) compile(tree Node) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
c.add(InstructionNil)
|
||||
c.putU16(jumpOverElse, uint16(c.ip-jumpOverElse-2))
|
||||
}
|
||||
|
||||
c.putU16(jumpOverElse, uint16(c.ip-jumpOverElse-2))
|
||||
|
||||
case LoopNodeType:
|
||||
n := tree.(*LoopNode)
|
||||
|
||||
|
|
@ -385,7 +353,7 @@ func (c *Compiler) compile(tree Node) error {
|
|||
}
|
||||
|
||||
alwaysLoop := false
|
||||
if c.optimize && c.isTreeConstant(n.condition) {
|
||||
if c.isTreeConstant(n.condition) {
|
||||
v, err := c.compute(n.condition)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -400,8 +368,6 @@ func (c *Compiler) compile(tree Node) error {
|
|||
}
|
||||
}
|
||||
|
||||
c.add(InstructionNil)
|
||||
|
||||
conditionPos := c.ip
|
||||
jumpValuePos := Pos(0)
|
||||
if !alwaysLoop {
|
||||
|
|
@ -415,8 +381,6 @@ func (c *Compiler) compile(tree Node) error {
|
|||
c.advance(2)
|
||||
}
|
||||
|
||||
c.add(InstructionPop)
|
||||
|
||||
err = c.compile(n.do)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -433,47 +397,26 @@ func (c *Compiler) compile(tree Node) error {
|
|||
case AssignNodeType:
|
||||
n := tree.(*AssignNode)
|
||||
|
||||
switch n.dest.Type() {
|
||||
case ReferenceNodeType:
|
||||
d := n.dest.(*ReferenceNode)
|
||||
|
||||
if d.name == "_" {
|
||||
return c.compile(n.value)
|
||||
}
|
||||
|
||||
if n.declare && c.isVarDeclaredHere(d.name) {
|
||||
return c.error(fmt.Sprintf("%s is already declared in this scope", d.name), n)
|
||||
}
|
||||
|
||||
if err := c.addSetVar(d.name, n.value, n.declare); err != nil {
|
||||
if n.name == "_" {
|
||||
// allow non-ish statements
|
||||
err := c.compile(n.value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.add(InstructionPop)
|
||||
} else {
|
||||
if n.declare && c.isVarDeclaredHere(n.name) {
|
||||
return c.error(fmt.Sprintf("%s is already declared in this scope", n.name), n)
|
||||
}
|
||||
|
||||
err := c.addSetVar(n.name, n.value, n.declare)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return c.error(fmt.Sprintf("cannot assign to %s", n.dest.Type()), n.dest)
|
||||
}
|
||||
|
||||
/*
|
||||
if n.name == "_" {
|
||||
// allow non-ish statements
|
||||
err := c.compile(n.value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.add(InstructionPop)
|
||||
} else {
|
||||
if n.declare && c.isVarDeclaredHere(n.name) {
|
||||
return c.error(fmt.Sprintf("%s is already declared in this scope", n.name), n)
|
||||
}
|
||||
|
||||
err := c.addSetVar(n.name, n.value, n.declare)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
case InvokeNodeType:
|
||||
n := tree.(*InvokeNode)
|
||||
case CallNodeType:
|
||||
n := tree.(*CallNode)
|
||||
|
||||
s, err := c.deduceSignature(n.source)
|
||||
if err != nil {
|
||||
|
|
@ -485,6 +428,10 @@ func (c *Compiler) compile(tree Node) error {
|
|||
return c.error(fmt.Sprintf("cannot call non-function value of type %s", s), n)
|
||||
}
|
||||
|
||||
if !n.keep && f.Out.Type() != TypeNil {
|
||||
c.warn(fmt.Sprintf("shouldn't void result of function call (output is non-nil %s)", f.Out), n)
|
||||
}
|
||||
|
||||
if len(n.args) != len(f.In) {
|
||||
return c.error(fmt.Sprintf("wrong argument count: function of signature %s got %d, requires %d", f, len(n.args), len(f.In)), n)
|
||||
}
|
||||
|
|
@ -516,7 +463,7 @@ func (c *Compiler) compile(tree Node) error {
|
|||
return c.error(fmt.Sprintf("argument #%d does not have expected type signature: got %s, requires %s", i, sig, f.In[i]), arg)
|
||||
}
|
||||
|
||||
if c.optimize && c.isTreeConstant(arg) {
|
||||
if c.isTreeConstant(arg) {
|
||||
v, err := c.compute(arg)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -539,6 +486,10 @@ func (c *Compiler) compile(tree Node) error {
|
|||
|
||||
c.add(InstructionCall)
|
||||
|
||||
if !n.keep {
|
||||
c.add(InstructionPop)
|
||||
}
|
||||
|
||||
case FunctionNodeType:
|
||||
n := tree.(*FunctionNode)
|
||||
|
||||
|
|
@ -587,7 +538,6 @@ func (c *Compiler) compile(tree Node) error {
|
|||
n.yield,
|
||||
c.Chunk,
|
||||
nil,
|
||||
nil,
|
||||
}
|
||||
|
||||
// restore old chunk and ip
|
||||
|
|
@ -623,7 +573,7 @@ func (c *Compiler) compile(tree Node) error {
|
|||
}
|
||||
|
||||
func (c *Compiler) compileBinary(binary *BinaryNode) error {
|
||||
if c.optimize && c.isTreeConstant(binary) {
|
||||
if c.isTreeConstant(binary) {
|
||||
v, err := c.compute(binary)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -643,77 +593,38 @@ func (c *Compiler) compileBinary(binary *BinaryNode) error {
|
|||
return err
|
||||
}
|
||||
|
||||
res, err := c.deduceSignature(binary)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
inType, err := c.deduceSignature(binary.Left) // type(left) == type(right) because res is valid
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch binary.BinaryOperation {
|
||||
case BinaryAddition:
|
||||
res, err := c.deduceSignature(binary)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Type() == TypeString {
|
||||
c.add(InstructionStringConcatenation)
|
||||
} else if res.Type() == TypeList {
|
||||
c.add(InstructionConcatLists)
|
||||
} else if res.Type() == TypeFloat {
|
||||
c.add(InstructionAddFloat)
|
||||
} else if res.Type() == TypeInteger {
|
||||
c.add(InstructionAddInt)
|
||||
} else {
|
||||
return c.error("unimplemented binary compilation", binary)
|
||||
c.add(InstructionAdd)
|
||||
}
|
||||
case BinarySubtraction:
|
||||
if res.Type() == TypeFloat {
|
||||
c.add(InstructionSubFloat)
|
||||
} else {
|
||||
c.add(InstructionSubInt)
|
||||
}
|
||||
c.add(InstructionSub)
|
||||
case BinaryMultiplication:
|
||||
if res.Type() == TypeFloat {
|
||||
c.add(InstructionMulFloat)
|
||||
} else {
|
||||
c.add(InstructionMulInt)
|
||||
}
|
||||
c.add(InstructionMul)
|
||||
case BinaryDivision:
|
||||
if res.Type() == TypeFloat {
|
||||
c.add(InstructionDivFloat)
|
||||
} else {
|
||||
c.add(InstructionDivInt)
|
||||
}
|
||||
c.add(InstructionDiv)
|
||||
case BinaryEquality:
|
||||
c.add(InstructionEquals)
|
||||
case BinaryInequality:
|
||||
c.add(InstructionNotEqual)
|
||||
|
||||
case BinaryLess:
|
||||
if inType.Type() == TypeFloat {
|
||||
c.add(InstructionLessFloat)
|
||||
} else {
|
||||
c.add(InstructionLessInt)
|
||||
}
|
||||
c.add(InstructionLess)
|
||||
case BinaryGreater:
|
||||
if inType.Type() == TypeFloat {
|
||||
c.add(InstructionGreaterFloat)
|
||||
} else {
|
||||
c.add(InstructionGreaterInt)
|
||||
}
|
||||
c.add(InstructionGreater)
|
||||
case BinaryLessEqual:
|
||||
if inType.Type() == TypeFloat {
|
||||
c.add(InstructionLessOrEqualFloat)
|
||||
} else {
|
||||
c.add(InstructionLessOrEqualInt)
|
||||
}
|
||||
c.add(InstructionLessOrEqual)
|
||||
case BinaryGreaterEqual:
|
||||
if inType.Type() == TypeFloat {
|
||||
c.add(InstructionGreaterOrEqualFloat)
|
||||
} else {
|
||||
c.add(InstructionGreaterOrEqualInt)
|
||||
}
|
||||
|
||||
c.add(InstructionGreaterOrEqual)
|
||||
case BinaryAnd:
|
||||
c.add(InstructionAnd)
|
||||
case BinaryOr:
|
||||
|
|
@ -727,10 +638,8 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
|
|||
switch tree.Type() {
|
||||
case StringNodeType:
|
||||
return &StringSignature{}, nil
|
||||
case FloatNodeType:
|
||||
return &FloatSignature{}, nil
|
||||
case IntegerNodeType:
|
||||
return &IntegerSignature{}, nil
|
||||
case NumberNodeType:
|
||||
return &NumberSignature{}, nil
|
||||
case ReferenceNodeType:
|
||||
n := tree.(*ReferenceNode)
|
||||
sig, err := c.getVarSignature(n.name, n)
|
||||
|
|
@ -786,23 +695,17 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
|
|||
|
||||
switch n.BinaryOperation {
|
||||
case BinarySubtraction, BinaryMultiplication, BinaryDivision:
|
||||
if l.Type() == TypeInteger {
|
||||
return &IntegerSignature{}, nil
|
||||
if l.Type() != TypeNumber {
|
||||
return nil, c.error(fmt.Sprintf("cannot %s values of non-number type %s", n.BinaryOperation, l), n)
|
||||
}
|
||||
|
||||
if l.Type() == TypeFloat {
|
||||
return &FloatSignature{}, nil
|
||||
}
|
||||
|
||||
return nil, c.error(fmt.Sprintf("cannot %s values of non-number type %s", n.BinaryOperation, l), n)
|
||||
return &NumberSignature{}, nil
|
||||
case BinaryAddition:
|
||||
switch l.Type() {
|
||||
case TypeString:
|
||||
return &StringSignature{}, nil
|
||||
case TypeInteger:
|
||||
return &IntegerSignature{}, nil
|
||||
case TypeFloat:
|
||||
return &FloatSignature{}, nil
|
||||
case TypeNumber:
|
||||
return &NumberSignature{}, nil
|
||||
case TypeList:
|
||||
return &ListSignature{
|
||||
l.(*ListSignature).Contents,
|
||||
|
|
@ -819,7 +722,7 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
|
|||
case BinaryEquality, BinaryInequality:
|
||||
return &BooleanSignature{}, nil
|
||||
case BinaryLess, BinaryGreater, BinaryLessEqual, BinaryGreaterEqual:
|
||||
if l.Type() != TypeInteger && l.Type() != TypeFloat {
|
||||
if l.Type() != TypeNumber {
|
||||
return nil, c.error(fmt.Sprintf("cannot perform number comparison (%s) on non-number type %s", n.BinaryOperation, l), n)
|
||||
}
|
||||
|
||||
|
|
@ -865,8 +768,8 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
|
|||
return nil, c.error(fmt.Sprintf("cannot access property from value of type %s", sig), n)
|
||||
}
|
||||
|
||||
case InvokeNodeType:
|
||||
n := tree.(*InvokeNode)
|
||||
case CallNodeType:
|
||||
n := tree.(*CallNode)
|
||||
sig, err := c.deduceSignature(n.source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -949,13 +852,10 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
|
|||
|
||||
switch n.UnaryOperation {
|
||||
case UnaryNegate:
|
||||
if sig.Type() == TypeFloat {
|
||||
return &FloatSignature{}, nil
|
||||
} else if sig.Type() == TypeInteger {
|
||||
return &IntegerSignature{}, nil
|
||||
if sig.Type() != TypeNumber {
|
||||
return nil, c.error(fmt.Sprintf("cannot perform negation on type %s (must be number)", n.UnaryOperation), n)
|
||||
}
|
||||
|
||||
return nil, c.error(fmt.Sprintf("cannot perform negation on type %s (must be number)", n.UnaryOperation), n)
|
||||
return &NumberSignature{}, nil
|
||||
case UnaryNot:
|
||||
if sig.Type() != TypeBoolean {
|
||||
return nil, c.error(fmt.Sprintf("cannot perform negation on type %s (must be boolean)", n.UnaryOperation), n)
|
||||
|
|
@ -964,15 +864,6 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
|
|||
}
|
||||
|
||||
return nil, c.error(fmt.Sprintf("unimplemented result type deduction for unary %s", n.UnaryOperation), n)
|
||||
case BlockNodeType:
|
||||
n := tree.(*BlockNode)
|
||||
if len(n.statements) == 0 {
|
||||
return &NilSignature{}, nil
|
||||
}
|
||||
|
||||
return c.deduceSignature(n.statements[len(n.statements)-1])
|
||||
case AssignNodeType:
|
||||
return c.deduceSignature(tree.(*AssignNode).value)
|
||||
default:
|
||||
return nil, c.error(fmt.Sprintf("impossible to deduce signature of %s", tree.Type()), tree)
|
||||
}
|
||||
|
|
@ -1038,37 +929,29 @@ func (c *Compiler) affirmReturnSignature(tree Node, sig TypeSignature) error {
|
|||
case AssignNodeType:
|
||||
n := tree.(*AssignNode)
|
||||
|
||||
switch n.dest.Type() {
|
||||
case ReferenceNodeType:
|
||||
name := n.dest.(*ReferenceNode).name
|
||||
|
||||
if !n.declare {
|
||||
prev, err := c.getVarSignature(name, n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sig, err := c.deduceSignature(n.value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !sig.Matches(prev) {
|
||||
return c.error(fmt.Sprintf("cannot assign value of type %s to variable %s of type %s", sig, name, prev), n.value)
|
||||
}
|
||||
|
||||
return nil
|
||||
if !n.declare {
|
||||
prev, err := c.getVarSignature(n.name, n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sig, err := c.deduceSignature(n.value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.registerVar(name, sig)
|
||||
default:
|
||||
return c.error("can neither assign nor declare to", n.dest)
|
||||
|
||||
if !sig.Matches(prev) {
|
||||
return c.error(fmt.Sprintf("cannot assign value of type %s to variable %s of type %s", sig, n.name, prev), n.value)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
sig, err := c.deduceSignature(n.value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.registerVar(n.name, sig)
|
||||
default:
|
||||
}
|
||||
|
||||
|
|
@ -1157,7 +1040,7 @@ func (c *Compiler) isLocal(name string) bool {
|
|||
// isTreeConstant check if a node tree is constant (predictable)
|
||||
func (c *Compiler) isTreeConstant(tree Node) bool {
|
||||
switch tree.Type() {
|
||||
case StringNodeType, FloatNodeType, IntegerNodeType, BooleanNodeType, NilNodeType:
|
||||
case StringNodeType, NumberNodeType, BooleanNodeType, NilNodeType:
|
||||
return true
|
||||
case ListNodeType:
|
||||
for _, item := range tree.(*ListNode).items {
|
||||
|
|
@ -1171,13 +1054,13 @@ func (c *Compiler) isTreeConstant(tree Node) bool {
|
|||
return c.isTreeConstant(tree.(*UnaryNode).value)
|
||||
case BinaryNodeType:
|
||||
return c.isTreeConstant(tree.(*BinaryNode).Left) && c.isTreeConstant(tree.(*BinaryNode).Right)
|
||||
case InvokeNodeType:
|
||||
for _, arg := range tree.(*InvokeNode).args {
|
||||
case CallNodeType:
|
||||
for _, arg := range tree.(*CallNode).args {
|
||||
if !c.isTreeConstant(arg) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return c.isTreeConstant(tree.(*InvokeNode).source)
|
||||
return c.isTreeConstant(tree.(*CallNode).source)
|
||||
case BlockNodeType, ConditionalNodeType, LoopNodeType, AssignNodeType, FunctionNodeType,
|
||||
ReturnNodeType, AccessNodeType, BreakpointNodeType, ReferenceNodeType:
|
||||
return false
|
||||
|
|
@ -1193,13 +1076,8 @@ func (c *Compiler) compute(tree Node) (Value, error) {
|
|||
n.value,
|
||||
}, nil
|
||||
|
||||
case *FloatNode:
|
||||
return &FloatValue{
|
||||
n.value,
|
||||
}, nil
|
||||
|
||||
case *IntegerNode:
|
||||
return &IntegerValue{
|
||||
case *NumberNode:
|
||||
return &NumberValue{
|
||||
n.value,
|
||||
}, nil
|
||||
|
||||
|
|
@ -1236,17 +1114,13 @@ func (c *Compiler) compute(tree Node) (Value, error) {
|
|||
|
||||
switch n.UnaryOperation {
|
||||
case UnaryNegate:
|
||||
if v.Type() == FloatValueType {
|
||||
return &FloatValue{
|
||||
-v.(*FloatValue).Number,
|
||||
}, nil
|
||||
} else if v.Type() == IntegerValueType {
|
||||
return &IntegerValue{
|
||||
new(big.Int).Neg(v.(*IntegerValue).Number),
|
||||
}, nil
|
||||
if v.Type() != NumberValueType {
|
||||
return nil, c.error(fmt.Sprintf("cannot negate %s value (not a number)", v.Type()), n)
|
||||
}
|
||||
|
||||
return nil, c.error(fmt.Sprintf("cannot negate %s value (not a number)", v.Type()), n)
|
||||
return &NumberValue{
|
||||
-v.(*NumberValue).Number,
|
||||
}, nil
|
||||
case UnaryNot:
|
||||
if v.Type() != BoolValueType {
|
||||
return nil, c.error(fmt.Sprintf("cannot invert %s value (not a boolean)", v.Type()), n)
|
||||
|
|
@ -1259,7 +1133,7 @@ func (c *Compiler) compute(tree Node) (Value, error) {
|
|||
|
||||
return nil, c.error(fmt.Sprintf("unimplemented unary %s", v.Type()), n)
|
||||
|
||||
case *InvokeNode:
|
||||
case *CallNode:
|
||||
source, err := c.compute(n.source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -1306,7 +1180,7 @@ func (c *Compiler) computeBinary(n *BinaryNode) (Value, error) {
|
|||
// perform type check
|
||||
switch n.BinaryOperation {
|
||||
case BinarySubtraction, BinaryMultiplication, BinaryDivision, BinaryLess, BinaryGreater, BinaryLessEqual, BinaryGreaterEqual:
|
||||
if l.Type() != FloatValueType && l.Type() != IntegerValueType {
|
||||
if l.Type() != NumberValueType {
|
||||
return nil, c.error(fmt.Sprintf("cannot %s values of non-number type %s", n.BinaryOperation, l.Type()), n)
|
||||
}
|
||||
case BinaryAnd, BinaryOr:
|
||||
|
|
@ -1322,67 +1196,37 @@ func (c *Compiler) computeBinary(n *BinaryNode) (Value, error) {
|
|||
switch n.BinaryOperation {
|
||||
case BinaryAddition:
|
||||
switch l.Type() {
|
||||
case FloatValueType:
|
||||
v = l.(*FloatValue).Number + r.(*FloatValue).Number
|
||||
case NumberValueType:
|
||||
v = l.(*NumberValue).Number + r.(*NumberValue).Number
|
||||
case StringValueType:
|
||||
v = l.(*StringValue).Text + r.(*StringValue).Text
|
||||
case ListValueType:
|
||||
v = append(l.(*ListValue).Items, r.(*ListValue).Items...)
|
||||
case IntegerValueType:
|
||||
v = new(big.Int).Add(l.(*IntegerValue).Number, r.(*IntegerValue).Number)
|
||||
default:
|
||||
return nil, c.error(fmt.Sprintf("cannot add values of type %s", l.Type()), n)
|
||||
}
|
||||
case BinarySubtraction:
|
||||
if l.Type() == FloatValueType {
|
||||
v = l.(*FloatValue).Number - r.(*FloatValue).Number
|
||||
} else {
|
||||
v = new(big.Int).Sub(l.(*IntegerValue).Number, r.(*IntegerValue).Number)
|
||||
}
|
||||
v = l.(*NumberValue).Number - r.(*NumberValue).Number
|
||||
case BinaryMultiplication:
|
||||
if l.Type() == FloatValueType {
|
||||
v = l.(*FloatValue).Number * r.(*FloatValue).Number
|
||||
} else {
|
||||
v = new(big.Int).Mul(l.(*IntegerValue).Number, r.(*IntegerValue).Number)
|
||||
}
|
||||
v = l.(*NumberValue).Number * r.(*NumberValue).Number
|
||||
case BinaryDivision:
|
||||
if l.Type() == FloatValueType {
|
||||
v = l.(*FloatValue).Number / r.(*FloatValue).Number
|
||||
} else {
|
||||
v = new(big.Int).Div(l.(*IntegerValue).Number, r.(*IntegerValue).Number)
|
||||
}
|
||||
v = l.(*NumberValue).Number / r.(*NumberValue).Number
|
||||
case BinaryAnd:
|
||||
v = l.(*BoolValue).Boolean && r.(*BoolValue).Boolean
|
||||
case BinaryOr:
|
||||
v = l.(*BoolValue).Boolean || r.(*BoolValue).Boolean
|
||||
v = l.(*BoolValue).Boolean && r.(*BoolValue).Boolean
|
||||
case BinaryEquality:
|
||||
v = l.Equals(r)
|
||||
case BinaryInequality:
|
||||
v = !l.Equals(r)
|
||||
case BinaryLess:
|
||||
if l.Type() == FloatValueType {
|
||||
v = l.(*FloatValue).Number < r.(*FloatValue).Number
|
||||
} else {
|
||||
v = l.(*IntegerValue).Number.Cmp(r.(*IntegerValue).Number) == -1
|
||||
}
|
||||
v = l.(*NumberValue).Number < r.(*NumberValue).Number
|
||||
case BinaryGreater:
|
||||
if l.Type() == FloatValueType {
|
||||
v = l.(*FloatValue).Number > r.(*FloatValue).Number
|
||||
} else {
|
||||
v = l.(*IntegerValue).Number.Cmp(r.(*IntegerValue).Number) == 1
|
||||
}
|
||||
v = l.(*NumberValue).Number > r.(*NumberValue).Number
|
||||
case BinaryLessEqual:
|
||||
if l.Type() == FloatValueType {
|
||||
v = l.(*FloatValue).Number <= r.(*FloatValue).Number
|
||||
} else {
|
||||
v = l.(*IntegerValue).Number.Cmp(r.(*IntegerValue).Number) != 1
|
||||
}
|
||||
v = l.(*NumberValue).Number <= r.(*NumberValue).Number
|
||||
case BinaryGreaterEqual:
|
||||
if l.Type() == FloatValueType {
|
||||
v = l.(*FloatValue).Number >= r.(*FloatValue).Number
|
||||
} else {
|
||||
v = l.(*IntegerValue).Number.Cmp(r.(*IntegerValue).Number) != 1
|
||||
}
|
||||
v = l.(*NumberValue).Number >= r.(*NumberValue).Number
|
||||
}
|
||||
|
||||
return GoToValue(v), nil
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ func BenchmarkNewCompiler(b *testing.B) {
|
|||
type CompileTestData struct {
|
||||
program *Program
|
||||
expectedStack []Value
|
||||
expectedScope []map[string]Value
|
||||
}
|
||||
|
||||
func GetCompileTestData() map[string]CompileTestData {
|
||||
|
|
@ -41,7 +40,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
"a",
|
||||
&StringNode{
|
||||
"Hello world!",
|
||||
"\"Hello world!\"",
|
||||
|
|
@ -55,22 +54,22 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
},
|
||||
"",
|
||||
},
|
||||
[]Value{&StringValue{"Hello world!"}},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &StringValue{"Hello world!"},
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&StringValue{"Hello world!"},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
/* these tests are so fucking unmaintainable
|
||||
"conditional_false": {
|
||||
&Program{
|
||||
[]Import{},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
0,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -85,8 +84,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -107,7 +106,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&FloatValue{0},
|
||||
&NumberValue{0},
|
||||
0,
|
||||
},
|
||||
},
|
||||
|
|
@ -118,8 +117,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
0,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -134,8 +133,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -156,7 +155,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
0,
|
||||
},
|
||||
},
|
||||
|
|
@ -167,8 +166,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
0,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -183,8 +182,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -197,8 +196,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -218,7 +217,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&FloatValue{2},
|
||||
&NumberValue{2},
|
||||
0,
|
||||
},
|
||||
},
|
||||
|
|
@ -229,8 +228,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
0,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -245,8 +244,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -259,8 +258,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
&FloatNode{
|
||||
"a",
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -280,7 +279,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
0,
|
||||
},
|
||||
},
|
||||
|
|
@ -291,14 +290,14 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
"a",
|
||||
&BinaryNode{
|
||||
BinaryAddition,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -315,7 +314,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&FloatValue{3},
|
||||
&NumberValue{3},
|
||||
0,
|
||||
},
|
||||
},
|
||||
|
|
@ -326,20 +325,20 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{name: "sum"},
|
||||
"sum",
|
||||
&FunctionNode{
|
||||
"sum",
|
||||
[]FunctionParameter{
|
||||
{
|
||||
"a",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
{
|
||||
"b",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&ReturnNode{
|
||||
|
|
@ -379,20 +378,20 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
[]FunctionParameter{
|
||||
{
|
||||
"a",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
{
|
||||
"b",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
NewChunk(
|
||||
[]Bytecode{
|
||||
InstructionDescend,
|
||||
InstructionGetLocal, 0,
|
||||
InstructionGetLocal, 1,
|
||||
InstructionAddFloat,
|
||||
InstructionAdd,
|
||||
InstructionReturn,
|
||||
InstructionAscend,
|
||||
},
|
||||
|
|
@ -412,16 +411,16 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
"a",
|
||||
&FunctionNode{
|
||||
"a",
|
||||
[]FunctionParameter{},
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"b", 0, 0},
|
||||
&FloatNode{
|
||||
"b",
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -449,6 +448,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
0, 0,
|
||||
},
|
||||
[]Node{},
|
||||
false,
|
||||
0, 0,
|
||||
},
|
||||
},
|
||||
|
|
@ -462,7 +462,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&FunctionValue{
|
||||
"a",
|
||||
[]FunctionParameter{},
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
NewChunk(
|
||||
[]Bytecode{
|
||||
InstructionDescend,
|
||||
|
|
@ -473,7 +473,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
InstructionAscend,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1}, &StringValue{"b"},
|
||||
&NumberValue{1}, &StringValue{"b"},
|
||||
},
|
||||
),
|
||||
nil,
|
||||
|
|
@ -488,17 +488,17 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
statements: []Node{
|
||||
&AssignNode{
|
||||
dest: &ReferenceNode{"a", 0, 0},
|
||||
name: "a",
|
||||
value: &ListNode{
|
||||
items: []Node{
|
||||
&FloatNode{value: 1},
|
||||
&FloatNode{value: 2},
|
||||
&NumberNode{value: 1},
|
||||
&NumberNode{value: 2},
|
||||
},
|
||||
},
|
||||
declare: true,
|
||||
},
|
||||
&AssignNode{
|
||||
dest: &ReferenceNode{"b", 0, 0},
|
||||
name: "b",
|
||||
value: &ListNode{
|
||||
items: []Node{
|
||||
&StringNode{value: "Hello"},
|
||||
|
|
@ -516,8 +516,8 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
name: "a",
|
||||
value: &ListValue{
|
||||
Items: []Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
},
|
||||
},
|
||||
scope: 0,
|
||||
|
|
@ -540,10 +540,10 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
dest: &ReferenceNode{"a", 0, 0},
|
||||
name: "a",
|
||||
value: &UnaryNode{
|
||||
UnaryNegate,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -559,7 +559,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
expectedStack: []Value{
|
||||
&VariableValue{
|
||||
name: "a",
|
||||
value: &FloatValue{-1},
|
||||
value: &NumberValue{-1},
|
||||
scope: 0,
|
||||
},
|
||||
},
|
||||
|
|
@ -570,7 +570,7 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
dest: &ReferenceNode{"a", 0, 0},
|
||||
name: "a",
|
||||
value: &UnaryNode{
|
||||
UnaryNot,
|
||||
&BooleanNode{
|
||||
|
|
@ -593,7 +593,6 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
},
|
||||
},
|
||||
},
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -642,8 +641,7 @@ func TestCompile(t *testing.T) {
|
|||
}
|
||||
t.Log("Executed bytecode")
|
||||
|
||||
CompareStacks(t, testCase.expectedStack, vm.Stack)
|
||||
CompareScope(t, testCase.expectedScope, vm.scope)
|
||||
CompareStacks(t, testCase.expectedStack, vm.stack)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -693,8 +691,8 @@ func TestCompiler_CleanStack(t *testing.T) {
|
|||
}
|
||||
|
||||
// make sure stack has only assigned values
|
||||
for i := 1; i < int(vm.Stack.Current); i++ {
|
||||
v := vm.Stack.items[i]
|
||||
for i := 0; i < int(vm.stack.Current); i++ {
|
||||
v := vm.stack.items[i]
|
||||
|
||||
if v == nil || v.Type() != VariableValueType {
|
||||
t.Errorf("Unclean stack! value %v at %d on the stack is intermediary", v.String(), i)
|
||||
|
|
|
|||
|
|
@ -9,13 +9,13 @@ import (
|
|||
type Token struct {
|
||||
Type TokenType
|
||||
Start Pos
|
||||
End Pos
|
||||
Length Pos
|
||||
Line Pos
|
||||
Lexeme string
|
||||
}
|
||||
|
||||
func (t Token) String() string {
|
||||
return fmt.Sprintf("token %s, '%s' %d -> %d, line %d", t.Type.String(), t.Lexeme, t.Start, t.End, t.Line)
|
||||
return fmt.Sprintf("token %s, '%s' %d -> %d, line %d", t.Type.String(), t.Lexeme, t.Start, t.Length, t.Line)
|
||||
}
|
||||
|
||||
type TokenType uint64
|
||||
|
|
@ -28,8 +28,7 @@ const (
|
|||
TokenBang
|
||||
TokenSemicolon
|
||||
|
||||
TokenInteger
|
||||
TokenFloat
|
||||
TokenNumber
|
||||
TokenHexadecimal
|
||||
TokenString
|
||||
TokenName
|
||||
|
|
@ -56,7 +55,6 @@ const (
|
|||
TokenComma
|
||||
TokenDot
|
||||
TokenColon
|
||||
TokenArrow
|
||||
|
||||
TokenAssign
|
||||
TokenDeclare
|
||||
|
|
@ -71,7 +69,6 @@ const (
|
|||
TokenPipe
|
||||
TokenDoublePipe
|
||||
|
||||
TokenNewLine
|
||||
TokenBreakpoint
|
||||
TokenEOF
|
||||
TokenError
|
||||
|
|
@ -89,10 +86,8 @@ func (t TokenType) String() string {
|
|||
return "slash"
|
||||
case TokenBang:
|
||||
return "bang"
|
||||
case TokenFloat:
|
||||
return "float"
|
||||
case TokenInteger:
|
||||
return "integer"
|
||||
case TokenNumber:
|
||||
return "number"
|
||||
case TokenString:
|
||||
return "string"
|
||||
case TokenTrue:
|
||||
|
|
@ -140,7 +135,7 @@ func (t TokenType) String() string {
|
|||
case TokenDeclare:
|
||||
return "declare"
|
||||
case TokenFunc:
|
||||
return "fn"
|
||||
return "func"
|
||||
case TokenReturn:
|
||||
return "return"
|
||||
case TokenWhile:
|
||||
|
|
@ -167,10 +162,6 @@ func (t TokenType) String() string {
|
|||
return "pipe"
|
||||
case TokenHexadecimal:
|
||||
return "hexadecimal"
|
||||
case TokenArrow:
|
||||
return "arrow"
|
||||
case TokenNewLine:
|
||||
return "newline"
|
||||
}
|
||||
|
||||
panic("UNDEFINED TOKENTYPE STRING CONVERSION")
|
||||
|
|
@ -215,14 +206,9 @@ func (l *Lexer) NextToken() (Token, error) {
|
|||
l.advance()
|
||||
|
||||
switch c {
|
||||
case '\n':
|
||||
return l.makeToken(TokenNewLine), nil
|
||||
case '+':
|
||||
return l.makeToken(TokenPlus), nil
|
||||
case '-':
|
||||
if l.accept('>') {
|
||||
return l.makeToken(TokenArrow), nil
|
||||
}
|
||||
return l.makeToken(TokenMinus), nil
|
||||
case '*':
|
||||
return l.makeToken(TokenStar), nil
|
||||
|
|
@ -353,7 +339,7 @@ func (l *Lexer) NextToken() (Token, error) {
|
|||
return l.makeToken(TokenElse), nil
|
||||
case "var":
|
||||
return l.makeToken(TokenVar), nil
|
||||
case "fn":
|
||||
case "func":
|
||||
return l.makeToken(TokenFunc), nil
|
||||
case "while":
|
||||
return l.makeToken(TokenWhile), nil
|
||||
|
|
@ -378,7 +364,7 @@ func (l *Lexer) NextToken() (Token, error) {
|
|||
return l.makeToken(TokenHexadecimal), nil
|
||||
}
|
||||
|
||||
return l.makeToken(TokenInteger), nil
|
||||
return l.makeToken(TokenNumber), nil
|
||||
} else if unicode.IsDigit(c) {
|
||||
for unicode.IsDigit(l.peek()) {
|
||||
l.advance()
|
||||
|
|
@ -389,22 +375,20 @@ func (l *Lexer) NextToken() (Token, error) {
|
|||
for unicode.IsDigit(l.peek()) {
|
||||
l.advance()
|
||||
}
|
||||
return l.makeToken(TokenFloat), nil
|
||||
}
|
||||
|
||||
return l.makeToken(TokenInteger), nil
|
||||
|
||||
return l.makeToken(TokenNumber), nil
|
||||
}
|
||||
|
||||
return l.makeToken(TokenError), errors.New(fmt.Sprintf("invalid token %c", c))
|
||||
}
|
||||
}
|
||||
|
||||
func NewToken(t TokenType, start Pos, end Pos, line Pos, lexeme string) Token {
|
||||
func NewToken(t TokenType, start Pos, length Pos, line Pos, lexeme string) Token {
|
||||
return Token{
|
||||
Type: t,
|
||||
Start: start,
|
||||
End: end,
|
||||
Length: length,
|
||||
Line: line,
|
||||
Lexeme: lexeme,
|
||||
}
|
||||
|
|
@ -426,7 +410,7 @@ func (l *Lexer) Tokenize() ([]Token, error) {
|
|||
}
|
||||
|
||||
func (l *Lexer) makeToken(t TokenType) Token {
|
||||
return NewToken(t, l.start, l.current, l.line, string(l.src[l.start:l.current]))
|
||||
return NewToken(t, l.start, l.current-l.start, l.line, string(l.src[l.start:l.current]))
|
||||
}
|
||||
|
||||
func (l *Lexer) peek() rune {
|
||||
|
|
@ -475,7 +459,7 @@ func (l *Lexer) isAtEnd() bool {
|
|||
}
|
||||
|
||||
func (l *Lexer) skipWhitespace() {
|
||||
for !l.isAtEnd() && unicode.IsSpace(l.peek()) && l.peek() != '\n' {
|
||||
for !l.isAtEnd() && unicode.IsSpace(l.peek()) {
|
||||
l.advance()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,33 +21,33 @@ func GetLexerTestData() map[string]LexerTestData {
|
|||
},
|
||||
"simple number(1)": {
|
||||
"1024",
|
||||
[]TokenType{TokenInteger, TokenEOF},
|
||||
[]TokenType{TokenNumber, TokenEOF},
|
||||
},
|
||||
"simple_arithmetics(7)": {
|
||||
"1 + 23 / 4 * 3",
|
||||
[]TokenType{
|
||||
TokenInteger, TokenPlus, TokenInteger, TokenSlash,
|
||||
TokenInteger, TokenStar, TokenInteger, TokenEOF,
|
||||
TokenNumber, TokenPlus, TokenNumber, TokenSlash,
|
||||
TokenNumber, TokenStar, TokenNumber, TokenEOF,
|
||||
},
|
||||
},
|
||||
"condition(3)": {
|
||||
"a <= 200",
|
||||
[]TokenType{TokenName, TokenLessThanOrEqual, TokenInteger, TokenEOF},
|
||||
[]TokenType{TokenName, TokenLessThanOrEqual, TokenNumber, TokenEOF},
|
||||
},
|
||||
"if_statement(10)": {
|
||||
"if a >= 200 {\n write(\"Hello world!\")\n}",
|
||||
[]TokenType{
|
||||
TokenIf, TokenName, TokenGreaterThanOrEqual, TokenInteger, TokenOpenBrace, TokenNewLine,
|
||||
TokenName, TokenOpenParenthesis, TokenString, TokenCloseParenthesis, TokenNewLine,
|
||||
TokenCloseBrace, TokenEOF,
|
||||
TokenIf, TokenName, TokenGreaterThanOrEqual, TokenNumber, TokenOpenBrace,
|
||||
TokenName, TokenOpenParenthesis, TokenString, TokenCloseParenthesis, TokenCloseBrace,
|
||||
TokenEOF,
|
||||
},
|
||||
},
|
||||
"if_else_statement(20)": {
|
||||
"if 23 * 2/3 > 32 {\n write(\"It is larger!\")\n} else {\n write(\"It is lower!\")\n}",
|
||||
[]TokenType{
|
||||
TokenIf, TokenInteger, TokenStar, TokenInteger, TokenSlash, TokenInteger, TokenGreaterThan, TokenInteger, TokenOpenBrace, TokenNewLine,
|
||||
TokenName, TokenOpenParenthesis, TokenString, TokenCloseParenthesis, TokenNewLine, TokenCloseBrace,
|
||||
TokenElse, TokenOpenBrace, TokenNewLine, TokenName, TokenOpenParenthesis, TokenString, TokenCloseParenthesis, TokenNewLine, TokenCloseBrace,
|
||||
TokenIf, TokenNumber, TokenStar, TokenNumber, TokenSlash, TokenNumber, TokenGreaterThan, TokenNumber, TokenOpenBrace,
|
||||
TokenName, TokenOpenParenthesis, TokenString, TokenCloseParenthesis, TokenCloseBrace,
|
||||
TokenElse, TokenOpenBrace, TokenName, TokenOpenParenthesis, TokenString, TokenCloseParenthesis, TokenCloseBrace,
|
||||
TokenEOF,
|
||||
},
|
||||
},
|
||||
|
|
@ -58,8 +58,8 @@ func GetLexerTestData() map[string]LexerTestData {
|
|||
"full_arithmetic_equality": {
|
||||
"a + 2 == 10 * 2 / 3",
|
||||
[]TokenType{
|
||||
TokenName, TokenPlus, TokenInteger, TokenEquals,
|
||||
TokenInteger, TokenStar, TokenInteger, TokenSlash, TokenInteger,
|
||||
TokenName, TokenPlus, TokenNumber, TokenEquals,
|
||||
TokenNumber, TokenStar, TokenNumber, TokenSlash, TokenNumber,
|
||||
TokenEOF,
|
||||
},
|
||||
},
|
||||
|
|
@ -77,7 +77,7 @@ func GetLexerTestData() map[string]LexerTestData {
|
|||
},
|
||||
"space_before_string": {
|
||||
"\n \"\"",
|
||||
[]TokenType{TokenNewLine, TokenString, TokenEOF},
|
||||
[]TokenType{TokenString, TokenEOF},
|
||||
},
|
||||
"write_call": {
|
||||
"write(\"Hello world\")",
|
||||
|
|
@ -86,46 +86,46 @@ func GetLexerTestData() map[string]LexerTestData {
|
|||
"complex_comparison": {
|
||||
"!(h__elo123 >= 1)",
|
||||
[]TokenType{
|
||||
TokenBang, TokenOpenParenthesis, TokenName, TokenGreaterThanOrEqual, TokenInteger, TokenCloseParenthesis,
|
||||
TokenBang, TokenOpenParenthesis, TokenName, TokenGreaterThanOrEqual, TokenNumber, TokenCloseParenthesis,
|
||||
TokenEOF,
|
||||
},
|
||||
},
|
||||
"3assignments_1condition": {
|
||||
"a = 8 * 32\nb = a > 256\nc = a <= 256\n!b == c",
|
||||
[]TokenType{
|
||||
TokenName, TokenAssign, TokenInteger, TokenStar, TokenInteger, TokenNewLine,
|
||||
TokenName, TokenAssign, TokenName, TokenGreaterThan, TokenInteger, TokenNewLine,
|
||||
TokenName, TokenAssign, TokenName, TokenLessThanOrEqual, TokenInteger, TokenNewLine,
|
||||
TokenName, TokenAssign, TokenNumber, TokenStar, TokenNumber,
|
||||
TokenName, TokenAssign, TokenName, TokenGreaterThan, TokenNumber,
|
||||
TokenName, TokenAssign, TokenName, TokenLessThanOrEqual, TokenNumber,
|
||||
TokenBang, TokenName, TokenEquals, TokenName, TokenEOF,
|
||||
},
|
||||
},
|
||||
"function": {
|
||||
"fn sum(a, b) {\n return a + b\n}",
|
||||
"func": {
|
||||
"func sum(a, b) {\n return a + b\n}",
|
||||
[]TokenType{
|
||||
TokenFunc, TokenName, TokenOpenParenthesis, TokenName, TokenComma, TokenName, TokenCloseParenthesis,
|
||||
TokenOpenBrace, TokenNewLine, TokenReturn, TokenName, TokenPlus, TokenName, TokenNewLine, TokenCloseBrace,
|
||||
TokenOpenBrace, TokenReturn, TokenName, TokenPlus, TokenName, TokenCloseBrace,
|
||||
},
|
||||
},
|
||||
"while_loop": {
|
||||
"while a < 5 {\n a = a + 1\n}",
|
||||
[]TokenType{
|
||||
TokenWhile, TokenName, TokenLessThan, TokenInteger, TokenOpenBrace, TokenNewLine,
|
||||
TokenName, TokenAssign, TokenName, TokenPlus, TokenInteger, TokenNewLine, TokenCloseBrace, TokenEOF,
|
||||
TokenWhile, TokenName, TokenLessThan, TokenNumber, TokenOpenBrace,
|
||||
TokenName, TokenAssign, TokenName, TokenPlus, TokenNumber, TokenCloseBrace,
|
||||
},
|
||||
},
|
||||
"lambda": {
|
||||
"sum := fn(a, b) {\n" +
|
||||
"sum := func(a, b) {\n" +
|
||||
" return a + b\n" +
|
||||
"}",
|
||||
[]TokenType{
|
||||
TokenName, TokenDeclare, TokenFunc, TokenOpenParenthesis, TokenName, TokenComma, TokenName, TokenCloseParenthesis,
|
||||
TokenOpenBrace, TokenNewLine, TokenReturn, TokenName, TokenPlus, TokenName, TokenNewLine, TokenCloseBrace,
|
||||
TokenOpenBrace, TokenReturn, TokenName, TokenPlus, TokenName, TokenCloseBrace,
|
||||
},
|
||||
},
|
||||
"list": {
|
||||
"data := [3, 1, 4, 1]",
|
||||
[]TokenType{
|
||||
TokenName, TokenDeclare, TokenOpenBracket, TokenInteger, TokenComma, TokenInteger, TokenComma, TokenInteger, TokenComma, TokenInteger, TokenCloseBracket,
|
||||
TokenName, TokenDeclare, TokenOpenBracket, TokenNumber, TokenComma, TokenNumber, TokenComma, TokenNumber, TokenComma, TokenNumber, TokenCloseBracket,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
119
core/nodes.go
119
core/nodes.go
|
|
@ -2,7 +2,6 @@ package core
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -22,20 +21,17 @@ type Boundary interface {
|
|||
|
||||
const (
|
||||
StringNodeType NodeType = iota
|
||||
FloatNodeType
|
||||
IntegerNodeType
|
||||
NumberNodeType
|
||||
ReferenceNodeType
|
||||
BooleanNodeType
|
||||
NilNodeType
|
||||
ListNodeType
|
||||
TupleNodeType
|
||||
BinaryNodeType
|
||||
UnaryNodeType
|
||||
BlockNodeType
|
||||
ConditionalNodeType
|
||||
LoopNodeType
|
||||
AssignNodeType
|
||||
InvokeNodeType
|
||||
CallNodeType
|
||||
FunctionNodeType
|
||||
ReturnNodeType
|
||||
|
|
@ -47,10 +43,8 @@ func (n NodeType) String() string {
|
|||
switch n {
|
||||
case StringNodeType:
|
||||
return "String"
|
||||
case FloatNodeType:
|
||||
return "Float"
|
||||
case IntegerNodeType:
|
||||
return "Integer"
|
||||
case NumberNodeType:
|
||||
return "Number"
|
||||
case ReferenceNodeType:
|
||||
return "Reference"
|
||||
case BinaryNodeType:
|
||||
|
|
@ -67,7 +61,7 @@ func (n NodeType) String() string {
|
|||
return "Loop"
|
||||
case AssignNodeType:
|
||||
return "Assign"
|
||||
case InvokeNodeType:
|
||||
case CallNodeType:
|
||||
return "Call"
|
||||
case FunctionNodeType:
|
||||
return "Function"
|
||||
|
|
@ -75,16 +69,12 @@ func (n NodeType) String() string {
|
|||
return "Return"
|
||||
case ListNodeType:
|
||||
return "List"
|
||||
case TupleNodeType:
|
||||
return "Tuple"
|
||||
case AccessNodeType:
|
||||
return "Access"
|
||||
case BreakpointNodeType:
|
||||
return "Breakpoint"
|
||||
case UnaryNodeType:
|
||||
return "Unary"
|
||||
case CallNodeType:
|
||||
return "Call"
|
||||
}
|
||||
return "Invalid Node Type"
|
||||
}
|
||||
|
|
@ -130,41 +120,22 @@ func (n StringNode) Bounds() (Pos, Pos) {
|
|||
return n.start, n.end
|
||||
}
|
||||
|
||||
type FloatNode struct {
|
||||
type NumberNode struct {
|
||||
value float64
|
||||
|
||||
start Pos
|
||||
end Pos
|
||||
}
|
||||
|
||||
func (n FloatNode) Type() NodeType {
|
||||
return FloatNodeType
|
||||
func (n NumberNode) Type() NodeType {
|
||||
return NumberNodeType
|
||||
}
|
||||
|
||||
func (n FloatNode) String() string {
|
||||
return strconv.FormatFloat(n.value, 'g', -1, FloatSize)
|
||||
func (n NumberNode) String() string {
|
||||
return strconv.FormatFloat(n.value, 'g', -1, NumberSize)
|
||||
}
|
||||
|
||||
func (n FloatNode) Bounds() (Pos, Pos) {
|
||||
return n.start, n.end
|
||||
}
|
||||
|
||||
type IntegerNode struct {
|
||||
value *big.Int
|
||||
|
||||
start Pos
|
||||
end Pos
|
||||
}
|
||||
|
||||
func (n IntegerNode) Type() NodeType {
|
||||
return IntegerNodeType
|
||||
}
|
||||
|
||||
func (n IntegerNode) String() string {
|
||||
return n.value.String()
|
||||
}
|
||||
|
||||
func (n IntegerNode) Bounds() (Pos, Pos) {
|
||||
func (n NumberNode) Bounds() (Pos, Pos) {
|
||||
return n.start, n.end
|
||||
}
|
||||
|
||||
|
|
@ -198,37 +169,6 @@ func (n ListNode) Bounds() (Pos, Pos) {
|
|||
return n.start, n.end
|
||||
}
|
||||
|
||||
type TupleNode struct {
|
||||
items []Node
|
||||
|
||||
start Pos
|
||||
end Pos
|
||||
}
|
||||
|
||||
func (n TupleNode) Type() NodeType {
|
||||
return TupleNodeType
|
||||
}
|
||||
|
||||
func (n TupleNode) String() string {
|
||||
sb := strings.Builder{}
|
||||
|
||||
sb.WriteString("(")
|
||||
for i, item := range n.items {
|
||||
if i > 0 {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
|
||||
sb.WriteString(item.String())
|
||||
}
|
||||
sb.WriteString(")")
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (n TupleNode) Bounds() (Pos, Pos) {
|
||||
return n.start, n.end
|
||||
}
|
||||
|
||||
type AccessNode struct {
|
||||
source Node
|
||||
property string
|
||||
|
|
@ -318,14 +258,14 @@ func (n BinaryOperation) Symbol() string {
|
|||
return "<"
|
||||
case BinaryGreater:
|
||||
return ">"
|
||||
case BinaryLessEqual:
|
||||
return "<="
|
||||
case BinaryGreaterEqual:
|
||||
return ">="
|
||||
case BinaryAnd:
|
||||
return "&&"
|
||||
case BinaryOr:
|
||||
return "||"
|
||||
case BinaryLessEqual:
|
||||
return "<="
|
||||
case BinaryGreaterEqual:
|
||||
return ">="
|
||||
}
|
||||
|
||||
panic("unsupported binary operation to symbol conversion for " + n.String())
|
||||
|
|
@ -516,7 +456,7 @@ func (n LoopNode) Bounds() (Pos, Pos) {
|
|||
|
||||
// AssignNode assignment
|
||||
type AssignNode struct {
|
||||
dest Node
|
||||
name string
|
||||
value Node
|
||||
declare bool
|
||||
|
||||
|
|
@ -529,39 +469,18 @@ func (n AssignNode) Type() NodeType {
|
|||
}
|
||||
|
||||
func (n AssignNode) String() string {
|
||||
return fmt.Sprintf("set %s to %s", n.dest, n.value)
|
||||
return fmt.Sprintf("set %s to %s", n.name, n.value)
|
||||
}
|
||||
|
||||
func (n AssignNode) Bounds() (Pos, Pos) {
|
||||
return n.start, n.end
|
||||
}
|
||||
|
||||
// InvokeNode function call
|
||||
type InvokeNode struct {
|
||||
source Node
|
||||
args []Node
|
||||
|
||||
start Pos
|
||||
end Pos
|
||||
}
|
||||
|
||||
func (n InvokeNode) Type() NodeType {
|
||||
return InvokeNodeType
|
||||
}
|
||||
|
||||
func (n InvokeNode) String() string {
|
||||
return fmt.Sprintf("invoke %s with args (%s)", n.source.String(), n.args)
|
||||
}
|
||||
|
||||
func (n InvokeNode) Bounds() (Pos, Pos) {
|
||||
return n.start, n.end
|
||||
}
|
||||
|
||||
// CallNode call a function of a value
|
||||
// CallNode function call
|
||||
type CallNode struct {
|
||||
source Node
|
||||
name Token
|
||||
args []Node
|
||||
keep bool
|
||||
|
||||
start Pos
|
||||
end Pos
|
||||
|
|
@ -572,7 +491,7 @@ func (n CallNode) Type() NodeType {
|
|||
}
|
||||
|
||||
func (n CallNode) String() string {
|
||||
return fmt.Sprintf("call %s on %s with args (%s)", n.name, n.source.String(), n.args)
|
||||
return fmt.Sprintf("call %s with args (%s)", n.source.String(), n.args)
|
||||
}
|
||||
|
||||
func (n CallNode) Bounds() (Pos, Pos) {
|
||||
|
|
|
|||
610
core/parser.go
610
core/parser.go
|
|
@ -4,7 +4,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -62,7 +61,7 @@ func (p ParsingError) Format() string {
|
|||
b.WriteRune(' ')
|
||||
}
|
||||
|
||||
for i := 0; i < len(p.Causer.Lexeme); i++ {
|
||||
for i := 0; i < int(p.Causer.Length); i++ {
|
||||
b.WriteRune('^')
|
||||
}
|
||||
b.WriteRune('\n')
|
||||
|
|
@ -76,13 +75,12 @@ func (p ParsingError) Format() string {
|
|||
}
|
||||
|
||||
type Parser struct {
|
||||
source string
|
||||
trace []string
|
||||
tokens []Token
|
||||
prev *Token
|
||||
curr *Token
|
||||
pos Pos
|
||||
ignoreNewLine bool
|
||||
source string
|
||||
trace []string
|
||||
tokens []Token
|
||||
prev *Token
|
||||
curr *Token
|
||||
pos Pos
|
||||
}
|
||||
|
||||
func NewParser(source string, trace []string, tokens []Token) *Parser {
|
||||
|
|
@ -144,19 +142,12 @@ func (p *Parser) Parse(path string) (*Program, error) {
|
|||
imports = append(imports, Import{
|
||||
p.prev.Lexeme[1 : len(p.prev.Lexeme)-1],
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
for p.accept(TokenNewLine) {
|
||||
}
|
||||
|
||||
if p.curr.Type == TokenEOF {
|
||||
break
|
||||
}
|
||||
|
||||
b, err := p.expression(false)
|
||||
b, err := p.block(true)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -172,7 +163,7 @@ func (p *Parser) Parse(path string) (*Program, error) {
|
|||
&BlockNode{
|
||||
statements,
|
||||
0,
|
||||
p.curr.End,
|
||||
p.curr.Start + p.curr.Length,
|
||||
},
|
||||
path,
|
||||
}, nil
|
||||
|
|
@ -184,12 +175,6 @@ func (p *Parser) accept(tokenType TokenType) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
if p.ignoreNewLine && tokenType != TokenNewLine {
|
||||
for p.curr.Type == TokenNewLine {
|
||||
p.advance()
|
||||
}
|
||||
}
|
||||
|
||||
if (*p.curr).Type == tokenType {
|
||||
p.advance()
|
||||
return true
|
||||
|
|
@ -218,11 +203,10 @@ func (p *Parser) advance() {
|
|||
|
||||
if p.pos < Pos(len(p.tokens)) {
|
||||
p.curr = &p.tokens[p.pos]
|
||||
p.pos++
|
||||
} else {
|
||||
p.curr = nil
|
||||
panic("no more tokens")
|
||||
}
|
||||
|
||||
p.pos++
|
||||
}
|
||||
|
||||
func (p *Parser) error(error string, causer *Token) error {
|
||||
|
|
@ -234,370 +218,6 @@ func (p *Parser) error(error string, causer *Token) error {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *Parser) expression(mustBeBlock bool) (Node, error) {
|
||||
if mustBeBlock || p.accept(TokenOpenBrace) {
|
||||
if mustBeBlock {
|
||||
if err := p.expect(TokenOpenBrace, "expected block"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
oldIgnoreNewline := p.ignoreNewLine
|
||||
p.ignoreNewLine = false
|
||||
|
||||
start := p.prev.Start
|
||||
|
||||
var statements []Node
|
||||
for !p.accept(TokenCloseBrace) {
|
||||
if p.accept(TokenNewLine) {
|
||||
continue
|
||||
}
|
||||
|
||||
s, err := p.expression(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
statements = append(statements, s)
|
||||
|
||||
if !p.accept(TokenNewLine) {
|
||||
if err := p.expect(TokenCloseBrace, "blocks must be closed"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
p.ignoreNewLine = oldIgnoreNewline
|
||||
|
||||
return &BlockNode{statements, start, p.prev.End}, nil
|
||||
}
|
||||
|
||||
t := p.curr
|
||||
switch t.Type {
|
||||
case TokenIf:
|
||||
p.advance()
|
||||
|
||||
cond, err := p.expression(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
do, err := p.expression(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var otherwise Node
|
||||
if p.accept(TokenElse) {
|
||||
otherwise, err = p.expression(p.curr.Type != TokenIf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &ConditionalNode{
|
||||
cond,
|
||||
do,
|
||||
otherwise,
|
||||
t.Start,
|
||||
t.End,
|
||||
}, nil
|
||||
|
||||
case TokenFunc:
|
||||
p.advance()
|
||||
start := p.prev.Start
|
||||
|
||||
var name *Token
|
||||
if p.accept(TokenName) { // can be unnamed, but accept name if it is named
|
||||
name = p.prev
|
||||
}
|
||||
|
||||
params, err := p.parseParams()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var yield TypeSignature
|
||||
if p.accept(TokenArrow) {
|
||||
yield, err = p.parseSignature()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
logic, err := p.expression(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
names := "*"
|
||||
if name != nil {
|
||||
names = name.Lexeme
|
||||
}
|
||||
|
||||
fn := &FunctionNode{
|
||||
names,
|
||||
params,
|
||||
yield,
|
||||
logic,
|
||||
start,
|
||||
p.prev.End,
|
||||
}
|
||||
|
||||
if name != nil {
|
||||
return &AssignNode{
|
||||
&ReferenceNode{name.Lexeme, name.Start, name.End},
|
||||
fn,
|
||||
true,
|
||||
start,
|
||||
p.prev.End,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return fn, nil
|
||||
|
||||
case TokenReturn:
|
||||
p.advance()
|
||||
start := p.prev.Start
|
||||
|
||||
v, err := p.expression(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ReturnNode{
|
||||
v,
|
||||
start,
|
||||
p.prev.End,
|
||||
}, nil
|
||||
|
||||
case TokenWhile:
|
||||
p.advance()
|
||||
start := p.prev.Start
|
||||
|
||||
cond, err := p.expression(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logic, err := p.expression(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &LoopNode{
|
||||
cond,
|
||||
logic,
|
||||
start,
|
||||
p.prev.End,
|
||||
}, nil
|
||||
|
||||
default:
|
||||
s, err := p.binary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if p.accept(TokenDeclare) || p.accept(TokenAssign) {
|
||||
isDeclaration := p.prev.Type == TokenDeclare
|
||||
|
||||
// possibly assign tuples; not implemented yet
|
||||
v, err := p.expression(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
start, _ := s.Bounds()
|
||||
_, end := v.Bounds()
|
||||
|
||||
return &AssignNode{
|
||||
s,
|
||||
v,
|
||||
isDeclaration,
|
||||
start,
|
||||
end,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
|
||||
func isBinaryOperator(tokenType TokenType) bool {
|
||||
switch tokenType {
|
||||
case TokenPlus, TokenMinus, TokenStar, TokenSlash, TokenPipe, TokenDoubleAmpersand, TokenDoublePipe, TokenEquals, TokenBangEquals, TokenLessThan, TokenLessThanOrEqual, TokenGreaterThan, TokenGreaterThanOrEqual:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func binaryPrecedence(op TokenType) int {
|
||||
switch op {
|
||||
case TokenDoubleAmpersand, TokenDoublePipe:
|
||||
return 1
|
||||
case TokenEquals, TokenBangEquals, TokenLessThan, TokenGreaterThan, TokenLessThanOrEqual, TokenGreaterThanOrEqual:
|
||||
return 2
|
||||
case TokenPlus, TokenMinus, TokenPipe:
|
||||
return 3
|
||||
case TokenStar, TokenSlash:
|
||||
return 5
|
||||
default:
|
||||
panic("unimplemented")
|
||||
}
|
||||
}
|
||||
|
||||
func tokenToBinaryOperation(tokenType TokenType) BinaryOperation {
|
||||
switch tokenType {
|
||||
case TokenPlus:
|
||||
return BinaryAddition
|
||||
case TokenMinus:
|
||||
return BinarySubtraction
|
||||
case TokenStar:
|
||||
return BinaryMultiplication
|
||||
case TokenSlash:
|
||||
return BinaryDivision
|
||||
case TokenPipe:
|
||||
panic("unimplemented bitwise ops")
|
||||
case TokenDoubleAmpersand:
|
||||
return BinaryAnd
|
||||
case TokenDoublePipe:
|
||||
return BinaryOr
|
||||
|
||||
case TokenEquals:
|
||||
return BinaryEquality
|
||||
case TokenBangEquals:
|
||||
return BinaryInequality
|
||||
case TokenLessThan:
|
||||
return BinaryLess
|
||||
case TokenLessThanOrEqual:
|
||||
return BinaryLessEqual
|
||||
case TokenGreaterThan:
|
||||
return BinaryGreater
|
||||
case TokenGreaterThanOrEqual:
|
||||
return BinaryGreaterEqual
|
||||
|
||||
default:
|
||||
panic("unimplemented")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Parser) binary() (Node, error) {
|
||||
t, err := p.chain()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ops := NewStack[*Token](128)
|
||||
values := NewStack[Node](256)
|
||||
values.pushItem(t)
|
||||
|
||||
for isBinaryOperator(p.curr.Type) {
|
||||
for ops.Current > 0 && binaryPrecedence(p.curr.Type) <= binaryPrecedence(ops.Peek().Type) {
|
||||
r := values.Pop()
|
||||
l := values.Pop()
|
||||
op := tokenToBinaryOperation(ops.Pop().Type)
|
||||
|
||||
start, _ := l.Bounds()
|
||||
_, end := r.Bounds()
|
||||
|
||||
values.Push(&BinaryNode{
|
||||
op,
|
||||
l,
|
||||
r,
|
||||
start,
|
||||
end,
|
||||
})
|
||||
}
|
||||
|
||||
ops.Push(p.curr)
|
||||
p.advance()
|
||||
|
||||
v, err := p.chain()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
values.Push(v)
|
||||
}
|
||||
|
||||
for ops.Current > 0 {
|
||||
r := values.Pop()
|
||||
l := values.Pop()
|
||||
op := tokenToBinaryOperation(ops.Pop().Type)
|
||||
|
||||
start, _ := l.Bounds()
|
||||
_, end := l.Bounds()
|
||||
|
||||
values.Push(&BinaryNode{
|
||||
op,
|
||||
l,
|
||||
r,
|
||||
start,
|
||||
end,
|
||||
})
|
||||
}
|
||||
|
||||
return values.Pop(), nil
|
||||
}
|
||||
|
||||
func (p *Parser) chain() (Node, error) {
|
||||
f, err := p.factor()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for {
|
||||
if p.accept(TokenDot) {
|
||||
if err = p.expect(TokenName, "can only access properties by name"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := p.prev
|
||||
|
||||
f = &AccessNode{
|
||||
f,
|
||||
p.prev.Lexeme,
|
||||
name.Start,
|
||||
name.End,
|
||||
}
|
||||
|
||||
if p.curr.Type == TokenOpenParenthesis {
|
||||
args, err := p.parseArgs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f = &InvokeNode{
|
||||
f,
|
||||
args,
|
||||
name.Start,
|
||||
p.prev.End,
|
||||
}
|
||||
}
|
||||
} else if p.curr.Type == TokenOpenParenthesis {
|
||||
start := p.curr.Start
|
||||
args, err := p.parseArgs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f = &InvokeNode{
|
||||
f,
|
||||
args,
|
||||
|
||||
start,
|
||||
p.prev.End,
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (p *Parser) factor() (Node, error) {
|
||||
switch (*p.curr).Type {
|
||||
case TokenString:
|
||||
|
|
@ -606,49 +226,35 @@ func (p *Parser) factor() (Node, error) {
|
|||
(*p.prev).Lexeme[1 : len((*p.prev).Lexeme)-1],
|
||||
(*p.prev).Lexeme,
|
||||
p.prev.Start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenInteger:
|
||||
case TokenNumber:
|
||||
p.advance()
|
||||
|
||||
num, success := new(big.Int).SetString(p.prev.Lexeme, 10)
|
||||
if !success {
|
||||
return nil, p.error(fmt.Sprintf("cannot parse integer base 10: %s", p.prev.Lexeme), p.prev)
|
||||
}
|
||||
|
||||
return &IntegerNode{
|
||||
num,
|
||||
p.prev.Start,
|
||||
p.prev.End,
|
||||
}, nil
|
||||
|
||||
case TokenFloat:
|
||||
p.advance()
|
||||
num, err := strconv.ParseFloat((*p.prev).Lexeme, FloatSize)
|
||||
num, err := strconv.ParseFloat((*p.prev).Lexeme, NumberSize)
|
||||
|
||||
if err != nil {
|
||||
return nil, p.error(fmt.Sprintf("Error parsing number: %v", err), p.prev)
|
||||
}
|
||||
|
||||
return &FloatNode{
|
||||
return &NumberNode{
|
||||
num,
|
||||
p.prev.Start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenHexadecimal:
|
||||
p.advance()
|
||||
start := (*p.prev).Start
|
||||
num, ok := new(big.Int).SetString(p.prev.Lexeme[2:], 16)
|
||||
if !ok {
|
||||
return nil, p.error(fmt.Sprintf("cannot parse hexadecimal: %v", p.prev.Lexeme), p.prev)
|
||||
num, err := strconv.ParseUint((*p.prev).Lexeme[2:], 16, NumberSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &IntegerNode{
|
||||
num,
|
||||
return &NumberNode{
|
||||
float64(num),
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenTrue:
|
||||
|
|
@ -656,14 +262,14 @@ func (p *Parser) factor() (Node, error) {
|
|||
return &BooleanNode{
|
||||
true,
|
||||
p.prev.Start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
case TokenFalse:
|
||||
p.advance()
|
||||
return &BooleanNode{
|
||||
false,
|
||||
p.prev.Start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenNil:
|
||||
|
|
@ -674,8 +280,6 @@ func (p *Parser) factor() (Node, error) {
|
|||
p.advance()
|
||||
start := p.prev.Start
|
||||
|
||||
// TODO: find better solution; current one is messy
|
||||
// Maybe perform better analysis to determine the kind of the list...
|
||||
if p.accept(TokenCloseBracket) {
|
||||
s, err := p.parseSignature()
|
||||
if err != nil {
|
||||
|
|
@ -686,13 +290,10 @@ func (p *Parser) factor() (Node, error) {
|
|||
[]Node{},
|
||||
s,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
oldIgnoreNewline := p.ignoreNewLine
|
||||
p.ignoreNewLine = true
|
||||
|
||||
var values []Node
|
||||
for !p.accept(TokenCloseBracket) {
|
||||
if len(values) > 0 {
|
||||
|
|
@ -709,13 +310,11 @@ func (p *Parser) factor() (Node, error) {
|
|||
values = append(values, value)
|
||||
}
|
||||
|
||||
p.ignoreNewLine = oldIgnoreNewline
|
||||
|
||||
return &ListNode{
|
||||
values,
|
||||
nil,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
// unary minus
|
||||
|
|
@ -731,7 +330,7 @@ func (p *Parser) factor() (Node, error) {
|
|||
UnaryNegate,
|
||||
f,
|
||||
first.Start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenBang:
|
||||
|
|
@ -747,14 +346,14 @@ func (p *Parser) factor() (Node, error) {
|
|||
UnaryNot,
|
||||
v,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenName:
|
||||
p.advance()
|
||||
name := (*p.prev).Lexeme
|
||||
start := p.prev.Start
|
||||
nameEnd := p.prev.End
|
||||
nameEnd := start + p.prev.Length
|
||||
|
||||
if p.curr.Type == TokenOpenParenthesis {
|
||||
args, err := p.parseArgs()
|
||||
|
|
@ -762,15 +361,16 @@ func (p *Parser) factor() (Node, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return &InvokeNode{
|
||||
return &CallNode{
|
||||
&ReferenceNode{
|
||||
name,
|
||||
start,
|
||||
nameEnd,
|
||||
},
|
||||
args,
|
||||
true,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -790,7 +390,7 @@ func (p *Parser) factor() (Node, error) {
|
|||
}
|
||||
|
||||
var sig TypeSignature = &NilSignature{}
|
||||
if p.accept(TokenArrow) {
|
||||
if p.curr.Type != TokenOpenBrace {
|
||||
sig, err = p.parseSignature()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -808,7 +408,7 @@ func (p *Parser) factor() (Node, error) {
|
|||
sig,
|
||||
b,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenOpenParenthesis:
|
||||
|
|
@ -823,16 +423,8 @@ func (p *Parser) factor() (Node, error) {
|
|||
|
||||
return v, nil
|
||||
|
||||
case TokenBreakpoint:
|
||||
p.advance()
|
||||
|
||||
return &BreakpointNode{
|
||||
p.prev.Start,
|
||||
p.prev.End,
|
||||
}, nil
|
||||
|
||||
default:
|
||||
return nil, p.error(fmt.Sprintf("invalid factor %s", p.curr), p.curr)
|
||||
return nil, p.error("invalid factor", p.curr)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -855,7 +447,7 @@ func (p *Parser) prop() (Node, error) {
|
|||
v,
|
||||
property,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}
|
||||
|
||||
// if called, also add
|
||||
|
|
@ -865,11 +457,12 @@ func (p *Parser) prop() (Node, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
v = &InvokeNode{
|
||||
v = &CallNode{
|
||||
v,
|
||||
args,
|
||||
true,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -901,7 +494,7 @@ func (p *Parser) product() (Node, error) {
|
|||
left,
|
||||
f,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -933,7 +526,7 @@ func (p *Parser) term() (Node, error) {
|
|||
left,
|
||||
pr,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -980,7 +573,7 @@ func (p *Parser) comparison() (Node, error) {
|
|||
left,
|
||||
t,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -1014,7 +607,7 @@ func (p *Parser) condition() (Node, error) {
|
|||
left,
|
||||
c,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -1053,18 +646,19 @@ func (p *Parser) statement() (Node, error) {
|
|||
then,
|
||||
otherwise,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenName:
|
||||
p.advance()
|
||||
name := p.prev
|
||||
start := p.prev.Start
|
||||
name := (*p.prev).Lexeme
|
||||
|
||||
if (*p.curr).Type == TokenDot {
|
||||
var v Node = &ReferenceNode{
|
||||
name.Lexeme,
|
||||
name.Start,
|
||||
name.End,
|
||||
name,
|
||||
start,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}
|
||||
|
||||
// parse chains of prop-getting ( "".split().join().length.round() )
|
||||
|
|
@ -1077,8 +671,8 @@ func (p *Parser) statement() (Node, error) {
|
|||
v = &AccessNode{
|
||||
v,
|
||||
property,
|
||||
name.Start,
|
||||
p.prev.End,
|
||||
start,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}
|
||||
|
||||
// if called, also add
|
||||
|
|
@ -1088,11 +682,12 @@ func (p *Parser) statement() (Node, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
v = &InvokeNode{
|
||||
v = &CallNode{
|
||||
v,
|
||||
args,
|
||||
name.Start,
|
||||
p.prev.End,
|
||||
(*p.curr).Type == TokenDot, // if the chain is continued, keep the value.
|
||||
start,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1104,15 +699,16 @@ func (p *Parser) statement() (Node, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return &InvokeNode{
|
||||
return &CallNode{
|
||||
&ReferenceNode{
|
||||
name.Lexeme,
|
||||
name.Start,
|
||||
name.End,
|
||||
name,
|
||||
start,
|
||||
start + Pos(len(name)),
|
||||
},
|
||||
args,
|
||||
name.Start,
|
||||
p.prev.End,
|
||||
false,
|
||||
start,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
} else if p.accept(TokenAssign) || p.accept(TokenDeclare) {
|
||||
isDeclaration := p.prev.Type == TokenDeclare
|
||||
|
|
@ -1121,16 +717,12 @@ func (p *Parser) statement() (Node, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return &AssignNode{ // THIS COULD BE MORE PERMISSIVE; its a new system
|
||||
&ReferenceNode{
|
||||
name.Lexeme,
|
||||
name.Start,
|
||||
name.End,
|
||||
},
|
||||
return &AssignNode{
|
||||
name,
|
||||
c,
|
||||
isDeclaration,
|
||||
name.Start,
|
||||
p.prev.End,
|
||||
start,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -1144,7 +736,7 @@ func (p *Parser) statement() (Node, error) {
|
|||
if err := p.expect(TokenName, "function must have a name"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := p.prev
|
||||
name := p.prev.Lexeme
|
||||
|
||||
params, err := p.parseParams()
|
||||
if err != nil {
|
||||
|
|
@ -1152,7 +744,7 @@ func (p *Parser) statement() (Node, error) {
|
|||
}
|
||||
|
||||
var yield TypeSignature = &NilSignature{}
|
||||
if p.accept(TokenArrow) {
|
||||
if p.curr.Type != TokenOpenBrace {
|
||||
yield, err = p.parseSignature()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -1165,22 +757,18 @@ func (p *Parser) statement() (Node, error) {
|
|||
}
|
||||
|
||||
return &AssignNode{
|
||||
&ReferenceNode{
|
||||
name.Lexeme,
|
||||
name.Start,
|
||||
name.End,
|
||||
},
|
||||
name,
|
||||
&FunctionNode{
|
||||
name.Lexeme,
|
||||
name,
|
||||
params,
|
||||
yield,
|
||||
b,
|
||||
funcStart,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
},
|
||||
true,
|
||||
funcStart,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenWhile:
|
||||
|
|
@ -1201,7 +789,7 @@ func (p *Parser) statement() (Node, error) {
|
|||
c,
|
||||
b,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenReturn:
|
||||
|
|
@ -1216,7 +804,7 @@ func (p *Parser) statement() (Node, error) {
|
|||
return &ReturnNode{
|
||||
c,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
|
||||
case TokenBreakpoint:
|
||||
|
|
@ -1266,7 +854,7 @@ func (p *Parser) block(canBeStatement bool) (Node, error) {
|
|||
return &BlockNode{
|
||||
statements,
|
||||
start,
|
||||
p.prev.End,
|
||||
p.prev.Start + p.prev.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -1278,7 +866,7 @@ func (p *Parser) parseArgs() ([]Node, error) {
|
|||
}
|
||||
|
||||
if !p.accept(TokenCloseParenthesis) {
|
||||
c, err := p.expression(false)
|
||||
c, err := p.condition()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1287,7 +875,7 @@ func (p *Parser) parseArgs() ([]Node, error) {
|
|||
if err := p.expect(TokenComma, "arguments must be separated by comma"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, err = p.expression(false)
|
||||
c, err = p.condition()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1354,17 +942,7 @@ func (p *Parser) parseParams() ([]FunctionParameter, error) {
|
|||
func (p *Parser) parseSignature() (TypeSignature, error) {
|
||||
var s TypeSignature
|
||||
|
||||
if p.accept(TokenOpenParenthesis) {
|
||||
is, err := p.parseSignature()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.expect(TokenCloseParenthesis, "expected closing parenthesis"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s = is
|
||||
} else if p.accept(TokenFunc) {
|
||||
if p.accept(TokenFunc) {
|
||||
if err := p.expect(TokenOpenParenthesis, "func signature must have parentheses for parameters"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1387,31 +965,15 @@ func (p *Parser) parseSignature() (TypeSignature, error) {
|
|||
in = append(in, sig)
|
||||
}
|
||||
|
||||
var out TypeSignature
|
||||
var err error
|
||||
if p.accept(TokenArrow) {
|
||||
out, err = p.parseSignature()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
out = &NilSignature{}
|
||||
out, err := p.parseSignature()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s = &FunctionSignature{
|
||||
in,
|
||||
out,
|
||||
}
|
||||
} else if p.accept(TokenOpenBracket) {
|
||||
inner, err := p.parseSignature()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.expect(TokenCloseBracket, "list type must be enclosed in brackets"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ListSignature{inner}, nil
|
||||
} else {
|
||||
if err := p.expect(TokenName, "type must be a name"); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -1421,10 +983,8 @@ func (p *Parser) parseSignature() (TypeSignature, error) {
|
|||
switch name {
|
||||
case "string":
|
||||
s = &StringSignature{}
|
||||
case "int":
|
||||
s = &IntegerSignature{}
|
||||
case "float":
|
||||
s = &FloatSignature{}
|
||||
case "number":
|
||||
s = &NumberSignature{}
|
||||
case "boolean":
|
||||
s = &BooleanSignature{}
|
||||
case "list":
|
||||
|
|
@ -1437,7 +997,7 @@ func (p *Parser) parseSignature() (TypeSignature, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := p.expect(TokenCloseBracket, "square brackets enclose list content signature"); err != nil {
|
||||
if err := p.expect(TokenCloseBracket, "list must close parameter list"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -55,22 +55,22 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
[]Token{
|
||||
NewToken(TokenName, 0, 1, 0, "_"),
|
||||
NewToken(TokenAssign, 1, 1, 0, "="),
|
||||
NewToken(TokenFloat, 3, 1, 0, "1"),
|
||||
NewToken(TokenNumber, 3, 1, 0, "1"),
|
||||
NewToken(TokenPlus, 4, 1, 0, "+"),
|
||||
NewToken(TokenFloat, 5, 1, 0, "2"),
|
||||
NewToken(TokenNumber, 5, 1, 0, "2"),
|
||||
NewToken(TokenEOF, 6, 0, 0, ""),
|
||||
},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"_", 0, 0},
|
||||
"_",
|
||||
&BinaryNode{
|
||||
BinaryAddition,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -93,7 +93,7 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"hello", 0, 0},
|
||||
"hello",
|
||||
&StringNode{
|
||||
"Hello world!",
|
||||
"\"Hello world!\"",
|
||||
|
|
@ -110,7 +110,7 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
[]Token{
|
||||
NewToken(TokenName, 0, 1, 0, "a"),
|
||||
NewToken(TokenDeclare, 1, 2, 0, ":="),
|
||||
NewToken(TokenFloat, 3, 1, 0, "1"),
|
||||
NewToken(TokenNumber, 3, 1, 0, "1"),
|
||||
NewToken(TokenPlus, 4, 1, 0, "+"),
|
||||
NewToken(TokenName, 5, 1, 0, "b"),
|
||||
NewToken(TokenEOF, 6, 0, 0, ""),
|
||||
|
|
@ -118,10 +118,10 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
"a",
|
||||
&BinaryNode{
|
||||
BinaryAddition,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -145,35 +145,35 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
NewToken(TokenAssign, 1, 2, 0, "="),
|
||||
|
||||
NewToken(TokenOpenParenthesis, 3, 1, 0, "("),
|
||||
NewToken(TokenFloat, 4, 1, 0, "2"),
|
||||
NewToken(TokenNumber, 4, 1, 0, "2"),
|
||||
NewToken(TokenPlus, 5, 1, 0, "+"),
|
||||
NewToken(TokenFloat, 6, 1, 0, "1"),
|
||||
NewToken(TokenNumber, 6, 1, 0, "1"),
|
||||
NewToken(TokenCloseParenthesis, 7, 1, 0, ")"),
|
||||
NewToken(TokenStar, 8, 1, 0, "*"),
|
||||
NewToken(TokenFloat, 9, 1, 0, "5"),
|
||||
NewToken(TokenNumber, 9, 1, 0, "5"),
|
||||
|
||||
NewToken(TokenPlus, 10, 1, 0, "+"),
|
||||
|
||||
NewToken(TokenFloat, 11, 1, 0, "3"),
|
||||
NewToken(TokenNumber, 11, 1, 0, "3"),
|
||||
NewToken(TokenSlash, 12, 1, 0, "/"),
|
||||
NewToken(TokenOpenParenthesis, 13, 1, 0, "("),
|
||||
NewToken(TokenFloat, 14, 1, 0, "6"),
|
||||
NewToken(TokenNumber, 14, 1, 0, "6"),
|
||||
NewToken(TokenMinus, 15, 1, 0, "-"),
|
||||
NewToken(TokenFloat, 16, 1, 0, "2"),
|
||||
NewToken(TokenNumber, 16, 1, 0, "2"),
|
||||
NewToken(TokenCloseParenthesis, 17, 1, 0, ")"),
|
||||
|
||||
NewToken(TokenMinus, 18, 1, 0, "-"),
|
||||
|
||||
NewToken(TokenFloat, 19, 2, 0, "10"),
|
||||
NewToken(TokenNumber, 19, 2, 0, "10"),
|
||||
NewToken(TokenSlash, 20, 1, 0, "/"),
|
||||
NewToken(TokenFloat, 21, 1, 0, "2"),
|
||||
NewToken(TokenNumber, 21, 1, 0, "2"),
|
||||
NewToken(TokenEOF, 22, 0, 0, ""),
|
||||
},
|
||||
// (2 + 1) * 5 + 3 / (6 - 2) - 10 / 2
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"_", 0, 0},
|
||||
"_",
|
||||
&BinaryNode{
|
||||
BinarySubtraction,
|
||||
&BinaryNode{
|
||||
|
|
@ -182,17 +182,17 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
BinaryMultiplication,
|
||||
&BinaryNode{
|
||||
BinaryAddition,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
5,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -200,17 +200,17 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
},
|
||||
&BinaryNode{
|
||||
BinaryDivision,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
3,
|
||||
0, 0,
|
||||
},
|
||||
&BinaryNode{
|
||||
BinarySubtraction,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
6,
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -222,11 +222,11 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
},
|
||||
&BinaryNode{
|
||||
BinaryDivision,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
10,
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -245,22 +245,22 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
[]Token{
|
||||
NewToken(TokenName, 0, 1, 0, "_"),
|
||||
NewToken(TokenAssign, 1, 1, 0, "="),
|
||||
NewToken(TokenFloat, 2, 2, 0, "20"),
|
||||
NewToken(TokenNumber, 2, 2, 0, "20"),
|
||||
NewToken(TokenEquals, 4, 2, 0, "=="),
|
||||
NewToken(TokenFloat, 6, 2, 0, "15"),
|
||||
NewToken(TokenNumber, 6, 2, 0, "15"),
|
||||
NewToken(TokenEOF, 8, 0, 0, ""),
|
||||
},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"_", 0, 0},
|
||||
"_",
|
||||
&BinaryNode{
|
||||
BinaryEquality,
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
20,
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
15,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -278,11 +278,11 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
NewToken(TokenIf, 0, 2, 0, "if"),
|
||||
NewToken(TokenName, 2, 1, 0, "a"),
|
||||
NewToken(TokenEquals, 3, 2, 0, "=="),
|
||||
NewToken(TokenFloat, 5, 1, 0, "0"),
|
||||
NewToken(TokenNumber, 5, 1, 0, "0"),
|
||||
NewToken(TokenOpenBrace, 6, 1, 0, "{"),
|
||||
NewToken(TokenName, 7, 1, 1, "b"),
|
||||
NewToken(TokenAssign, 8, 1, 1, "="),
|
||||
NewToken(TokenFloat, 9, 1, 1, "1"),
|
||||
NewToken(TokenNumber, 9, 1, 1, "1"),
|
||||
NewToken(TokenCloseBrace, 10, 1, 2, "}"),
|
||||
NewToken(TokenEOF, 11, 0, 2, ""),
|
||||
},
|
||||
|
|
@ -295,7 +295,7 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
"a",
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
0,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -304,8 +304,8 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
do: &BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"b", 0, 0},
|
||||
&FloatNode{
|
||||
"b",
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -325,17 +325,17 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
NewToken(TokenIf, 0, 2, 0, "if"),
|
||||
NewToken(TokenName, 2, 1, 0, "a"),
|
||||
NewToken(TokenEquals, 3, 2, 0, "=="),
|
||||
NewToken(TokenFloat, 5, 1, 0, "0"),
|
||||
NewToken(TokenNumber, 5, 1, 0, "0"),
|
||||
NewToken(TokenOpenBrace, 6, 1, 0, "{"),
|
||||
NewToken(TokenName, 7, 1, 1, "b"),
|
||||
NewToken(TokenAssign, 8, 1, 1, "="),
|
||||
NewToken(TokenFloat, 9, 1, 1, "1"),
|
||||
NewToken(TokenNumber, 9, 1, 1, "1"),
|
||||
NewToken(TokenCloseBrace, 10, 1, 2, "}"),
|
||||
NewToken(TokenElse, 11, 4, 2, "else"),
|
||||
NewToken(TokenOpenBrace, 15, 1, 2, "{"),
|
||||
NewToken(TokenName, 16, 1, 2, "b"),
|
||||
NewToken(TokenAssign, 17, 1, 2, "="),
|
||||
NewToken(TokenFloat, 18, 1, 2, "0"),
|
||||
NewToken(TokenNumber, 18, 1, 2, "0"),
|
||||
NewToken(TokenCloseBrace, 19, 1, 2, "}"),
|
||||
NewToken(TokenEOF, 20, 0, 2, ""),
|
||||
},
|
||||
|
|
@ -348,7 +348,7 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
"a",
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
0,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -357,8 +357,8 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
do: &BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"b", 0, 0},
|
||||
&FloatNode{
|
||||
"b",
|
||||
&NumberNode{
|
||||
1,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -371,8 +371,8 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
otherwise: &BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"b", 0, 0},
|
||||
&FloatNode{
|
||||
"b",
|
||||
&NumberNode{
|
||||
0,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -403,22 +403,21 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
0, 0,
|
||||
},
|
||||
},
|
||||
"lambda": { // a := fn(a: float, b: float) -> float { return a + b }
|
||||
"lambda": { // a := func(a, b) { return a + b }
|
||||
[]Token{
|
||||
NewToken(TokenName, 0, 1, 0, "a"),
|
||||
NewToken(TokenDeclare, 1, 2, 0, ":="),
|
||||
NewToken(TokenFunc, 3, 2, 0, "fn"),
|
||||
NewToken(TokenOpenParenthesis, 5, 1, 0, "("),
|
||||
NewToken(TokenName, 6, 1, 0, "a"),
|
||||
NewToken(TokenColon, 6, 1, 0, ":"),
|
||||
NewToken(TokenName, 10, 5, 0, "float"),
|
||||
NewToken(TokenFunc, 3, 4, 0, "func"),
|
||||
NewToken(TokenOpenParenthesis, 7, 1, 0, "("),
|
||||
NewToken(TokenName, 8, 1, 0, "a"),
|
||||
NewToken(TokenColon, 9, 1, 0, ":"),
|
||||
NewToken(TokenName, 10, 5, 0, "number"),
|
||||
NewToken(TokenComma, 9, 1, 0, ","),
|
||||
NewToken(TokenName, 10, 1, 0, "b"),
|
||||
NewToken(TokenColon, 9, 1, 0, ":"),
|
||||
NewToken(TokenName, 10, 5, 0, "float"),
|
||||
NewToken(TokenName, 10, 5, 0, "number"),
|
||||
NewToken(TokenCloseParenthesis, 11, 1, 0, ")"),
|
||||
NewToken(TokenArrow, 12, 1, 0, "->"),
|
||||
NewToken(TokenName, 10, 5, 0, "float"),
|
||||
NewToken(TokenName, 10, 5, 0, "number"),
|
||||
|
||||
NewToken(TokenOpenBrace, 12, 1, 1, "{"),
|
||||
NewToken(TokenReturn, 13, 6, 1, "return"),
|
||||
|
|
@ -432,20 +431,20 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
"a",
|
||||
&FunctionNode{
|
||||
"*",
|
||||
[]FunctionParameter{
|
||||
{
|
||||
"a",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
{
|
||||
"b",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&ReturnNode{
|
||||
|
|
@ -477,19 +476,13 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
},
|
||||
"function_declaration": {
|
||||
[]Token{
|
||||
NewToken(TokenFunc, 0, 2, 0, "fn"),
|
||||
NewToken(TokenFunc, 0, 4, 0, "func"),
|
||||
NewToken(TokenName, 4, 3, 0, "a"),
|
||||
NewToken(TokenOpenParenthesis, 7, 1, 0, "("),
|
||||
NewToken(TokenName, 8, 1, 0, "a"),
|
||||
NewToken(TokenColon, 9, 1, 0, ":"),
|
||||
NewToken(TokenName, 8, 1, 0, "float"),
|
||||
NewToken(TokenComma, 9, 1, 0, ","),
|
||||
NewToken(TokenName, 10, 1, 0, "b"),
|
||||
NewToken(TokenColon, 9, 1, 0, ":"),
|
||||
NewToken(TokenName, 8, 1, 0, "float"),
|
||||
NewToken(TokenCloseParenthesis, 11, 1, 0, ")"),
|
||||
NewToken(TokenArrow, 9, 1, 0, "->"),
|
||||
NewToken(TokenName, 8, 1, 0, "float"),
|
||||
|
||||
NewToken(TokenOpenBrace, 12, 1, 1, "{"),
|
||||
NewToken(TokenReturn, 13, 6, 1, "return"),
|
||||
|
|
@ -503,20 +496,20 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
"a",
|
||||
&FunctionNode{
|
||||
"a",
|
||||
[]FunctionParameter{
|
||||
{
|
||||
"a",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
{
|
||||
"b",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&ReturnNode{
|
||||
|
|
@ -559,7 +552,7 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"p", 0, 0},
|
||||
"p",
|
||||
&AccessNode{
|
||||
&ReferenceNode{
|
||||
"a",
|
||||
|
|
@ -584,7 +577,7 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
NewToken(TokenName, 8, 1, 0, "a"),
|
||||
NewToken(TokenComma, 11, 1, 0, ","),
|
||||
|
||||
NewToken(TokenFloat, 8, 1, 0, "3.141"),
|
||||
NewToken(TokenNumber, 8, 1, 0, "3.141"),
|
||||
NewToken(TokenComma, 11, 1, 0, ","),
|
||||
|
||||
NewToken(TokenString, 6, 1, 0, "\"Hello world!\""),
|
||||
|
|
@ -594,10 +587,10 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
NewToken(TokenComma, 11, 1, 0, ","),
|
||||
|
||||
NewToken(TokenOpenBracket, 6, 1, 0, "["),
|
||||
NewToken(TokenFloat, 6, 1, 0, "2"),
|
||||
NewToken(TokenNumber, 6, 1, 0, "2"),
|
||||
NewToken(TokenComma, 11, 1, 0, ","),
|
||||
|
||||
NewToken(TokenFloat, 6, 1, 0, "3"),
|
||||
NewToken(TokenNumber, 6, 1, 0, "3"),
|
||||
NewToken(TokenCloseBracket, 6, 1, 0, "]"),
|
||||
|
||||
NewToken(TokenCloseBracket, 6, 1, 0, "]"),
|
||||
|
|
@ -607,14 +600,14 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
&ReferenceNode{"data", 0, 0},
|
||||
"data",
|
||||
&ListNode{
|
||||
[]Node{
|
||||
&ReferenceNode{
|
||||
"a",
|
||||
0, 0,
|
||||
},
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
3.141,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -629,10 +622,10 @@ func GetTokenTestData() map[string]TokenTestData {
|
|||
},
|
||||
&ListNode{
|
||||
[]Node{
|
||||
&FloatNode{
|
||||
&NumberNode{
|
||||
2,
|
||||
0, 0,
|
||||
}, &FloatNode{
|
||||
}, &NumberNode{
|
||||
3,
|
||||
0, 0,
|
||||
},
|
||||
|
|
@ -684,20 +677,12 @@ func NodeEquality(t *testing.T, n1 Node, n2 Node) {
|
|||
t.Logf("String node quoted values match (%s)", n1.(*StringNode).value)
|
||||
}
|
||||
|
||||
case FloatNodeType:
|
||||
if n1.(*FloatNode).value != n2.(*FloatNode).value {
|
||||
t.Errorf("Float node values don't match (%f and %f)", n1.(*FloatNode).value, n2.(*FloatNode).value)
|
||||
case NumberNodeType:
|
||||
if n1.(*NumberNode).value != n2.(*NumberNode).value {
|
||||
t.Errorf("Number node values don't match (%f and %f)", n1.(*NumberNode).value, n2.(*NumberNode).value)
|
||||
} else {
|
||||
t.Logf("Float node values match (%f)", n1.(*FloatNode).value)
|
||||
t.Logf("Number node values match (%f)", n1.(*NumberNode).value)
|
||||
}
|
||||
|
||||
case IntegerNodeType:
|
||||
if n1.(*IntegerNode).value.Cmp(n2.(*IntegerNode).value) != 0 {
|
||||
t.Errorf("Integer node values don't match (%d and %d)", n1.(*IntegerNode).value, n2.(*IntegerNode).value)
|
||||
} else {
|
||||
t.Logf("Integer node values match (%d)", n1.(*IntegerNode).value)
|
||||
}
|
||||
|
||||
case ReferenceNodeType:
|
||||
if n1.(*ReferenceNode).name != n2.(*ReferenceNode).name {
|
||||
t.Errorf("Reference node values don't match (%s and %s)", n1.(*ReferenceNode).name, n2.(*ReferenceNode).name)
|
||||
|
|
@ -749,8 +734,11 @@ func NodeEquality(t *testing.T, n1 Node, n2 Node) {
|
|||
NodeEquality(t, n1.(*LoopNode).do, n2.(*LoopNode).do)
|
||||
|
||||
case AssignNodeType:
|
||||
t.Logf("Checking if value destination matches")
|
||||
NodeEquality(t, n1.(*AssignNode).dest, n2.(*AssignNode).dest)
|
||||
if n1.(*AssignNode).name != n2.(*AssignNode).name {
|
||||
t.Errorf("Assigned value name is not the same (%s and %s)", n1.(*AssignNode).name, n2.(*AssignNode).name)
|
||||
} else {
|
||||
t.Logf("Assigned value name matches (%s)", n1.(*AssignNode).name)
|
||||
}
|
||||
|
||||
if n1.(*AssignNode).declare != n2.(*AssignNode).declare {
|
||||
t.Errorf("Not same type of assigning (1: %v; 2: %v)", n1.(*AssignNode).declare, n2.(*AssignNode).declare)
|
||||
|
|
@ -759,18 +747,22 @@ func NodeEquality(t *testing.T, n1 Node, n2 Node) {
|
|||
t.Logf("Checking equality of assignment values")
|
||||
NodeEquality(t, n1.(*AssignNode).value, n2.(*AssignNode).value)
|
||||
|
||||
case InvokeNodeType:
|
||||
n := n1.(*InvokeNode)
|
||||
m := n2.(*InvokeNode)
|
||||
case CallNodeType:
|
||||
n := n1.(*CallNode)
|
||||
m := n2.(*CallNode)
|
||||
|
||||
NodeEquality(t, n.source, m.source)
|
||||
|
||||
if n.keep == m.keep {
|
||||
t.Logf("Call node keep modifier doesn't match (%v and %v)", n.keep, m.keep)
|
||||
}
|
||||
|
||||
if len(n.args) != len(m.args) {
|
||||
t.Fatalf("Call node arguments count does not match (%d and %d)", len(n.args), m.args)
|
||||
}
|
||||
|
||||
for i, arg := range m.args {
|
||||
NodeEquality(t, n1.(*InvokeNode).args[i], arg)
|
||||
NodeEquality(t, n1.(*CallNode).args[i], arg)
|
||||
}
|
||||
|
||||
case FunctionNodeType:
|
||||
|
|
@ -803,37 +795,6 @@ func NodeEquality(t *testing.T, n1 Node, n2 Node) {
|
|||
|
||||
case ReturnNodeType:
|
||||
NodeEquality(t, n1.(*ReturnNode).value, n2.(*ReturnNode).value)
|
||||
|
||||
case AccessNodeType:
|
||||
a1 := n1.(*AccessNode)
|
||||
a2 := n2.(*AccessNode)
|
||||
|
||||
if a1.property != a2.property {
|
||||
t.Errorf("Access node property does not match: .%s != .%s", a1.property, a2.property)
|
||||
} else {
|
||||
t.Logf("Access node property matches: .%s", a1.property)
|
||||
}
|
||||
|
||||
NodeEquality(t, a1.source, a2.source)
|
||||
|
||||
case ListNodeType:
|
||||
l1 := n1.(*ListNode)
|
||||
l2 := n2.(*ListNode)
|
||||
|
||||
if l1.content == nil && l2.content == nil {
|
||||
// fine
|
||||
t.Logf("Both content types are yet to be determined")
|
||||
} else if l1.content != nil || l2.content != nil {
|
||||
t.Errorf("one is nil, one is not")
|
||||
} else if !l1.content.Matches(l2.content) {
|
||||
t.Errorf("signature doesn't match")
|
||||
}
|
||||
|
||||
for i, v1 := range l1.items {
|
||||
t.Logf("Checking item %d", i)
|
||||
NodeEquality(t, v1, l2.items[i])
|
||||
}
|
||||
|
||||
default:
|
||||
panic("unimplemented node equality")
|
||||
}
|
||||
|
|
@ -857,7 +818,7 @@ func SerializeTokens(tokens []Token) string {
|
|||
out.WriteString("!")
|
||||
case TokenSemicolon:
|
||||
out.WriteString(";")
|
||||
case TokenFloat:
|
||||
case TokenNumber:
|
||||
out.WriteString(token.Lexeme)
|
||||
case TokenString:
|
||||
out.WriteString(fmt.Sprintf("\"%s\"", token.Lexeme))
|
||||
|
|
@ -884,7 +845,7 @@ func SerializeTokens(tokens []Token) string {
|
|||
case TokenNil:
|
||||
out.WriteString("nil")
|
||||
case TokenFunc:
|
||||
out.WriteString("fn")
|
||||
out.WriteString("func")
|
||||
case TokenReturn:
|
||||
out.WriteString("return ")
|
||||
case TokenWhile:
|
||||
|
|
@ -943,6 +904,10 @@ func TestParser_Parse(t *testing.T) {
|
|||
tokenData := GetTokenTestData()
|
||||
|
||||
for name, data := range tokenData {
|
||||
if name != "empty_block" && name != "lambda" {
|
||||
continue
|
||||
}
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Logf("Initializing parser")
|
||||
p := NewParser("", []string{}, data.tokens)
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ type Stack[T any] struct {
|
|||
items []T
|
||||
}
|
||||
|
||||
func NewStack[T any](maxCapacity Pos) *Stack[T] {
|
||||
func NewStack[T any](capacity Pos) *Stack[T] {
|
||||
return &Stack[T]{
|
||||
items: make([]T, min(maxCapacity, 16)),
|
||||
Capacity: maxCapacity,
|
||||
items: make([]T, 16),
|
||||
Capacity: capacity,
|
||||
Current: 0,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,30 +5,6 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func CompareScope(t *testing.T, expectedScope []map[string]Value, actualScope *Scope) {
|
||||
s := actualScope
|
||||
|
||||
for i := len(expectedScope) - 1; i >= 0; i-- {
|
||||
if s == nil {
|
||||
t.Fatal("scope cut unexpecetantly short")
|
||||
}
|
||||
|
||||
for name, value := range expectedScope[i] {
|
||||
v, ok := s.current[name]
|
||||
if !ok {
|
||||
t.Errorf("variable %s not found in correct scope", name)
|
||||
continue
|
||||
}
|
||||
|
||||
if !v.Equals(value) {
|
||||
t.Errorf("variable %s has value %s but expected %s", name, v.String(), value.String())
|
||||
} else {
|
||||
t.Logf("variable %s has expected value %s", name, v.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CompareStacks[T Value](t *testing.T, expected []T, actual *Stack[T]) {
|
||||
if actual.Current != Pos(len(expected)) {
|
||||
t.Errorf("Unexpected stack size. Expected %d, got %d", len(expected), actual.Current)
|
||||
|
|
|
|||
|
|
@ -9,8 +9,7 @@ type Type int
|
|||
|
||||
const (
|
||||
TypeString Type = iota
|
||||
TypeInteger
|
||||
TypeFloat
|
||||
TypeNumber
|
||||
TypeBoolean
|
||||
TypeNil
|
||||
TypeList
|
||||
|
|
@ -25,10 +24,8 @@ func (t Type) String() string {
|
|||
switch t {
|
||||
case TypeString:
|
||||
return "string"
|
||||
case TypeInteger:
|
||||
return "integer"
|
||||
case TypeFloat:
|
||||
return "float"
|
||||
case TypeNumber:
|
||||
return "number"
|
||||
case TypeBoolean:
|
||||
return "boolean"
|
||||
case TypeNil:
|
||||
|
|
@ -54,10 +51,8 @@ func SignatureOf(v Value) TypeSignature {
|
|||
switch t := v.(type) {
|
||||
case *StringValue:
|
||||
return &StringSignature{}
|
||||
case *FloatValue:
|
||||
return &FloatSignature{}
|
||||
case *IntegerValue:
|
||||
return &IntegerSignature{}
|
||||
case *NumberValue:
|
||||
return &NumberSignature{}
|
||||
case *BoolValue:
|
||||
return &BooleanSignature{}
|
||||
case *ListValue:
|
||||
|
|
@ -143,40 +138,22 @@ func (*StringSignature) String() string {
|
|||
return "string"
|
||||
}
|
||||
|
||||
type FloatSignature struct{}
|
||||
type NumberSignature struct{}
|
||||
|
||||
func (*FloatSignature) Type() Type {
|
||||
return TypeFloat
|
||||
func (*NumberSignature) Type() Type {
|
||||
return TypeNumber
|
||||
}
|
||||
|
||||
func (s *FloatSignature) Matches(other TypeSignature) bool {
|
||||
func (s *NumberSignature) Matches(other TypeSignature) bool {
|
||||
if other.Type() == TypeComposite {
|
||||
return other.Matches(s)
|
||||
}
|
||||
|
||||
return other.Type() == TypeAny || other.Type() == TypeFloat
|
||||
return other.Type() == TypeAny || other.Type() == TypeNumber
|
||||
}
|
||||
|
||||
func (*FloatSignature) String() string {
|
||||
return "float"
|
||||
}
|
||||
|
||||
type IntegerSignature struct{}
|
||||
|
||||
func (*IntegerSignature) Type() Type {
|
||||
return TypeInteger
|
||||
}
|
||||
|
||||
func (s *IntegerSignature) Matches(other TypeSignature) bool {
|
||||
if other.Type() == TypeComposite {
|
||||
return other.Matches(s)
|
||||
}
|
||||
|
||||
return other.Type() == TypeAny || other.Type() == TypeInteger
|
||||
}
|
||||
|
||||
func (*IntegerSignature) String() string {
|
||||
return "int"
|
||||
func (*NumberSignature) String() string {
|
||||
return "number"
|
||||
}
|
||||
|
||||
type BooleanSignature struct{}
|
||||
|
|
|
|||
139
core/values.go
139
core/values.go
|
|
@ -3,7 +3,7 @@ package core
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"math"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
|
@ -13,8 +13,7 @@ type ValueType int
|
|||
const (
|
||||
NilValueType ValueType = iota
|
||||
BoolValueType
|
||||
FloatValueType
|
||||
IntegerValueType
|
||||
NumberValueType
|
||||
StringValueType
|
||||
ListValueType
|
||||
ObjectValueType
|
||||
|
|
@ -31,10 +30,8 @@ func (v ValueType) String() string {
|
|||
return "bool"
|
||||
case ObjectValueType:
|
||||
return "object"
|
||||
case FloatValueType:
|
||||
return "float"
|
||||
case IntegerValueType:
|
||||
return "int"
|
||||
case NumberValueType:
|
||||
return "number"
|
||||
case StringValueType:
|
||||
return "string"
|
||||
case ListValueType:
|
||||
|
|
@ -60,15 +57,11 @@ func GoToValue(gov interface{}) Value {
|
|||
v,
|
||||
}
|
||||
case int:
|
||||
return &IntegerValue{
|
||||
new(big.Int).SetInt64(int64(v)),
|
||||
}
|
||||
case *big.Int:
|
||||
return &IntegerValue{
|
||||
v,
|
||||
return &NumberValue{
|
||||
float64(v),
|
||||
}
|
||||
case float64:
|
||||
return &FloatValue{
|
||||
return &NumberValue{
|
||||
v,
|
||||
}
|
||||
case string:
|
||||
|
|
@ -266,71 +259,40 @@ func (v *ObjectValue) Clone() Value {
|
|||
}
|
||||
}
|
||||
|
||||
// FloatValue floating-point values
|
||||
type FloatValue struct {
|
||||
// NumberValue Integer or floating-point values
|
||||
type NumberValue struct {
|
||||
Number float64
|
||||
}
|
||||
|
||||
const FloatSize int = 64
|
||||
const NumberSize int = 64
|
||||
|
||||
func (v *FloatValue) Type() ValueType {
|
||||
return FloatValueType
|
||||
func (v *NumberValue) Type() ValueType {
|
||||
return NumberValueType
|
||||
}
|
||||
|
||||
func (v *FloatValue) String() string {
|
||||
return strconv.FormatFloat(v.Number, 'g', -1, FloatSize)
|
||||
func (v *NumberValue) String() string {
|
||||
return strconv.FormatFloat(v.Number, 'g', -1, NumberSize)
|
||||
}
|
||||
|
||||
func (v *FloatValue) DebugString() string {
|
||||
func (v *NumberValue) DebugString() string {
|
||||
return v.String()
|
||||
}
|
||||
|
||||
func (v *FloatValue) Equals(other Value) bool {
|
||||
return other.Type() == FloatValueType && other.(*FloatValue).Number == v.Number
|
||||
func (v *NumberValue) Equals(other Value) bool {
|
||||
return other.Type() == NumberValueType && other.(*NumberValue).Number == v.Number
|
||||
}
|
||||
|
||||
func (v *FloatValue) Get(_ string) (Value, error) {
|
||||
func (v *NumberValue) Get(_ string) (Value, error) {
|
||||
// TODO maybe add standard functions for number values?
|
||||
return nil, errors.New("numbers have no properties")
|
||||
}
|
||||
|
||||
func (v *FloatValue) Clone() Value {
|
||||
return &FloatValue{
|
||||
func (v *NumberValue) Clone() Value {
|
||||
return &NumberValue{
|
||||
v.Number,
|
||||
}
|
||||
}
|
||||
|
||||
// IntegerValue whole number/integer values
|
||||
type IntegerValue struct {
|
||||
Number *big.Int
|
||||
}
|
||||
|
||||
func (v *IntegerValue) Type() ValueType {
|
||||
return IntegerValueType
|
||||
}
|
||||
|
||||
func (v *IntegerValue) String() string {
|
||||
return v.Number.String()
|
||||
}
|
||||
|
||||
func (v *IntegerValue) DebugString() string {
|
||||
return v.String()
|
||||
}
|
||||
|
||||
func (v *IntegerValue) Equals(other Value) bool {
|
||||
return other.Type() == IntegerValueType && other.(*IntegerValue).Number.Cmp(v.Number) == 0
|
||||
}
|
||||
|
||||
func (v *IntegerValue) Get(_ string) (Value, error) {
|
||||
return nil, errors.New("numbers have no properties")
|
||||
}
|
||||
|
||||
func (v *IntegerValue) Clone() Value {
|
||||
return &IntegerValue{
|
||||
new(big.Int).Set(v.Number),
|
||||
}
|
||||
}
|
||||
|
||||
type StringValue struct {
|
||||
Text string
|
||||
}
|
||||
|
|
@ -386,7 +348,7 @@ var StringPrototype = map[string]*BuiltinFunctionValue{
|
|||
Name: "length",
|
||||
Signature: &FunctionSignature{
|
||||
[]TypeSignature{},
|
||||
&IntegerSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
F: func(vm *VM, this Value, _ []Value) (Value, error) {
|
||||
return GoToValue(len(this.(*StringValue).Text)), nil
|
||||
|
|
@ -395,11 +357,11 @@ var StringPrototype = map[string]*BuiltinFunctionValue{
|
|||
"at": {
|
||||
Name: "at",
|
||||
Signature: &FunctionSignature{
|
||||
[]TypeSignature{&IntegerSignature{}},
|
||||
[]TypeSignature{&NumberSignature{}},
|
||||
&StringSignature{},
|
||||
},
|
||||
F: func(vm *VM, this Value, args []Value) (Value, error) {
|
||||
i := int(args[0].(*IntegerValue).Number.Int64())
|
||||
i := int(math.Floor(args[0].(*NumberValue).Number))
|
||||
|
||||
if i < 0 || i >= len(this.(*StringValue).Text) {
|
||||
return nil, errors.New("index is out of range")
|
||||
|
|
@ -488,13 +450,13 @@ var ListPrototype = map[string]*BuiltinFunctionValue{
|
|||
"at",
|
||||
&FunctionSignature{
|
||||
[]TypeSignature{
|
||||
&IntegerSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
&InnerSignature{},
|
||||
},
|
||||
func(_ *VM, this Value, p []Value) (Value, error) {
|
||||
items := this.(*ListValue).Items
|
||||
index := int(p[0].(*IntegerValue).Number.Int64())
|
||||
index := int(p[0].(*NumberValue).Number)
|
||||
|
||||
if index >= len(items) {
|
||||
return nil, errors.New(fmt.Sprintf("list index %x out of range", index))
|
||||
|
|
@ -509,13 +471,13 @@ var ListPrototype = map[string]*BuiltinFunctionValue{
|
|||
"put",
|
||||
&FunctionSignature{
|
||||
[]TypeSignature{
|
||||
&FloatSignature{}, &InnerSignature{},
|
||||
&NumberSignature{}, &InnerSignature{},
|
||||
},
|
||||
&NilSignature{},
|
||||
},
|
||||
func(_ *VM, this Value, args []Value) (Value, error) {
|
||||
l := this.(*ListValue)
|
||||
i := int(args[0].(*FloatValue).Number)
|
||||
i := int(args[0].(*NumberValue).Number)
|
||||
v := args[1]
|
||||
|
||||
// bounds check
|
||||
|
|
@ -534,7 +496,7 @@ var ListPrototype = map[string]*BuiltinFunctionValue{
|
|||
"length",
|
||||
&FunctionSignature{
|
||||
[]TypeSignature{},
|
||||
&IntegerSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
func(_ *VM, this Value, _ []Value) (Value, error) {
|
||||
return GoToValue(len(this.(*ListValue).Items)), nil
|
||||
|
|
@ -603,7 +565,6 @@ type FunctionValue struct {
|
|||
Yield TypeSignature
|
||||
Chunk *Chunk
|
||||
Parent Value
|
||||
Scope *Scope
|
||||
}
|
||||
|
||||
func (v *FunctionValue) Type() ValueType {
|
||||
|
|
@ -611,7 +572,7 @@ func (v *FunctionValue) Type() ValueType {
|
|||
}
|
||||
|
||||
func (v *FunctionValue) String() string {
|
||||
return fmt.Sprintf("<function name=%s block=%p>", v.Name, v.Chunk)
|
||||
return fmt.Sprintf("<function name=%s>", v.Name)
|
||||
}
|
||||
|
||||
func (v *FunctionValue) DebugString() string {
|
||||
|
|
@ -620,6 +581,7 @@ func (v *FunctionValue) DebugString() string {
|
|||
|
||||
func (v *FunctionValue) Equals(other Value) bool {
|
||||
return other.Type() == FunctionValueType &&
|
||||
v.Name == other.(*FunctionValue).Name &&
|
||||
v.Chunk == other.(*FunctionValue).Chunk
|
||||
}
|
||||
|
||||
|
|
@ -634,7 +596,6 @@ func (v *FunctionValue) Clone() Value {
|
|||
v.Yield,
|
||||
v.Chunk,
|
||||
v.Parent,
|
||||
v.Scope,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -676,3 +637,43 @@ func (v *BuiltinFunctionValue) Clone() Value {
|
|||
v.Constant,
|
||||
}
|
||||
}
|
||||
|
||||
// VariableValue a value wrapper for variables kept on the stack
|
||||
type VariableValue struct {
|
||||
name string
|
||||
value Value
|
||||
scope Pos
|
||||
}
|
||||
|
||||
func (v *VariableValue) Type() ValueType {
|
||||
return VariableValueType
|
||||
}
|
||||
|
||||
func (v *VariableValue) String() string {
|
||||
return fmt.Sprintf("<variable name=%s value=%s scope=%d>", v.name, v.value, v.scope)
|
||||
|
||||
// variables should not be accessed on the stack; normal values should be pushed and popped predictably
|
||||
//panic("tried getting string value of a unreachable value")
|
||||
}
|
||||
|
||||
func (v *VariableValue) DebugString() string {
|
||||
return v.String()
|
||||
}
|
||||
|
||||
func (v *VariableValue) Equals(other Value) bool {
|
||||
return other.Type() == VariableValueType &&
|
||||
v.name == other.(*VariableValue).name &&
|
||||
v.value.Equals(other.(*VariableValue).value)
|
||||
}
|
||||
|
||||
func (v *VariableValue) Get(_ string) (Value, error) {
|
||||
return nil, errors.New("variables have no properties")
|
||||
}
|
||||
|
||||
func (v *VariableValue) Clone() Value {
|
||||
return &VariableValue{
|
||||
v.name,
|
||||
v.value.Clone(),
|
||||
v.scope,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,21 +21,15 @@ func CompareValues(t *testing.T, got Value, want Value) {
|
|||
} else {
|
||||
t.Logf("Both are same boolean (%s)", want.(*BoolValue).String())
|
||||
}
|
||||
case FloatValueType:
|
||||
if got.(*FloatValue).Number != want.(*FloatValue).Number {
|
||||
t.Errorf("number value mismatch: got %v, want %v", got.(*FloatValue), want.(*FloatValue))
|
||||
case NumberValueType:
|
||||
if got.(*NumberValue).Number != want.(*NumberValue).Number {
|
||||
t.Errorf("number value mismatch: got %v, want %v", got.(*NumberValue), want.(*NumberValue))
|
||||
} else {
|
||||
t.Logf("Both are same number (%s)", got.(*FloatValue).String())
|
||||
}
|
||||
case IntegerValueType:
|
||||
if got.(*IntegerValue).Number.String() != want.(*IntegerValue).Number.String() {
|
||||
t.Errorf("number value mismatch: got %v, want %v", got.(*IntegerValue), want.(*IntegerValue))
|
||||
} else {
|
||||
t.Logf("Both are same number (%s)", got.(*IntegerValue).String())
|
||||
t.Logf("Both are same number (%s)", got.(*NumberValue).String())
|
||||
}
|
||||
case StringValueType:
|
||||
if got.(*StringValue).Text != want.(*StringValue).Text {
|
||||
t.Errorf("string value mismatch: got %s, want %s", got.(*StringValue), want.(*StringValue))
|
||||
t.Errorf("string value mismatch: got %v, want %v", got.(*StringValue), want.(*StringValue))
|
||||
} else {
|
||||
t.Logf("Both are same string (%s)", got.(*StringValue).String())
|
||||
}
|
||||
|
|
@ -70,6 +64,20 @@ func CompareValues(t *testing.T, got Value, want Value) {
|
|||
t.Errorf("builtin function parameter count mismatch: got %v, want %v", n, m)
|
||||
}
|
||||
|
||||
case VariableValueType:
|
||||
n := got.(*VariableValue)
|
||||
m := want.(*VariableValue)
|
||||
|
||||
if n.name != m.name {
|
||||
t.Errorf("variable name mismatch: got %v, want %v", n.name, m.name)
|
||||
}
|
||||
|
||||
if n.scope != m.scope {
|
||||
t.Errorf("variable scope mismatch: got %v, want %v", n.scope, m.scope)
|
||||
}
|
||||
|
||||
CompareValues(t, n.value, m.value)
|
||||
|
||||
case ListValueType:
|
||||
n := got.(*ListValue)
|
||||
m := want.(*ListValue)
|
||||
|
|
|
|||
661
core/vm.go
661
core/vm.go
File diff suppressed because it is too large
Load diff
202
core/vm_test.go
202
core/vm_test.go
|
|
@ -49,7 +49,7 @@ func TestNewVM(t *testing.T) {
|
|||
chunk := NewChunk([]Bytecode{
|
||||
InstructionConstant, 0,
|
||||
}, []Value{
|
||||
&FloatValue{0},
|
||||
&NumberValue{0},
|
||||
})
|
||||
stackSize := Pos(256)
|
||||
callstackSize := Pos(256)
|
||||
|
|
@ -76,8 +76,8 @@ func TestNewVM(t *testing.T) {
|
|||
}
|
||||
|
||||
// should have given stack size
|
||||
if vm.Stack.Capacity != stackSize {
|
||||
t.Errorf("vm.stack.Capacity = %d, want %d", vm.Stack.Capacity, stackSize)
|
||||
if vm.stack.Capacity != stackSize {
|
||||
t.Errorf("vm.stack.Capacity = %d, want %d", vm.stack.Capacity, stackSize)
|
||||
}
|
||||
|
||||
// should have given call stack size
|
||||
|
|
@ -95,26 +95,23 @@ func BenchmarkNewVM(b *testing.B) {
|
|||
func GetExecutionTestData() map[string]struct {
|
||||
chunk *Chunk
|
||||
resultingStack []Value
|
||||
resultingScope []map[string]Value
|
||||
} {
|
||||
return map[string]struct {
|
||||
chunk *Chunk
|
||||
resultingStack []Value
|
||||
resultingScope []map[string]Value
|
||||
}{
|
||||
"two_plus_one": {
|
||||
NewChunk([]Bytecode{
|
||||
InstructionConstant, 0,
|
||||
InstructionConstant, 1,
|
||||
InstructionAddFloat,
|
||||
InstructionAdd,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1}, &FloatValue{2},
|
||||
&NumberValue{1}, &NumberValue{2},
|
||||
}),
|
||||
[]Value{
|
||||
&FloatValue{3},
|
||||
&NumberValue{3},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"push_constant": {
|
||||
NewChunk(
|
||||
|
|
@ -122,13 +119,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionConstant, 0,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"push_true": {
|
||||
NewChunk(
|
||||
|
|
@ -140,7 +136,6 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{
|
||||
&BoolValue{true},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"push_false": {
|
||||
NewChunk(
|
||||
|
|
@ -152,7 +147,6 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{
|
||||
&BoolValue{false},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"push_nil": {
|
||||
NewChunk(
|
||||
|
|
@ -164,7 +158,6 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{
|
||||
&NilValue{},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"empty": {
|
||||
NewChunk(
|
||||
|
|
@ -172,7 +165,6 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{},
|
||||
),
|
||||
[]Value{},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
// (2 + 1) * 5 / (6 - 2)
|
||||
"full_arithmetic": {
|
||||
|
|
@ -180,22 +172,21 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Bytecode{
|
||||
InstructionConstant, 0,
|
||||
InstructionConstant, 1,
|
||||
InstructionAddFloat,
|
||||
InstructionAdd,
|
||||
InstructionConstant, 2,
|
||||
InstructionMulFloat,
|
||||
InstructionMul,
|
||||
InstructionConstant, 3,
|
||||
InstructionConstant, 0,
|
||||
InstructionSubFloat,
|
||||
InstructionDivFloat,
|
||||
InstructionSub,
|
||||
InstructionDiv,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{2}, &FloatValue{1}, &FloatValue{5}, &FloatValue{6},
|
||||
&NumberValue{2}, &NumberValue{1}, &NumberValue{5}, &NumberValue{6},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{3.75},
|
||||
&NumberValue{3.75},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"equality_true": {
|
||||
NewChunk(
|
||||
|
|
@ -205,13 +196,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionEquals,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&BoolValue{true},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"equality_false": {
|
||||
NewChunk(
|
||||
|
|
@ -221,13 +211,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionEquals,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1}, &FloatValue{2},
|
||||
&NumberValue{1}, &NumberValue{2},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&BoolValue{false},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"inequality_false": {
|
||||
NewChunk(
|
||||
|
|
@ -237,13 +226,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionNotEqual,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&BoolValue{false},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"inequality_true": {
|
||||
NewChunk(
|
||||
|
|
@ -253,13 +241,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionNotEqual,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1}, &FloatValue{2},
|
||||
&NumberValue{1}, &NumberValue{2},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&BoolValue{true},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"not_true": {
|
||||
NewChunk(
|
||||
|
|
@ -272,7 +259,6 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{
|
||||
&BoolValue{false},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"not_false": {
|
||||
NewChunk(
|
||||
|
|
@ -285,7 +271,6 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{
|
||||
&BoolValue{true},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"jump": {
|
||||
NewChunk(
|
||||
|
|
@ -295,13 +280,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionConstant, 1, // should execute
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &FloatValue{1},
|
||||
&NumberValue{0}, &NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"jump_false/false": {
|
||||
NewChunk(
|
||||
|
|
@ -312,13 +296,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionConstant, 1, // should execute
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &FloatValue{1},
|
||||
&NumberValue{0}, &NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&NumberValue{1},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"jump_false/true": {
|
||||
NewChunk(
|
||||
|
|
@ -329,13 +312,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionConstant, 1, // should execute
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &FloatValue{1},
|
||||
&NumberValue{0}, &NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{0}, &FloatValue{1},
|
||||
&NumberValue{0}, &NumberValue{1},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"declare_local": {
|
||||
NewChunk(
|
||||
|
|
@ -344,13 +326,14 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionDeclareLocal, 1,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &StringValue{"a"},
|
||||
&NumberValue{0}, &StringValue{"a"},
|
||||
},
|
||||
),
|
||||
[]Value{&FloatValue{0}},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &FloatValue{0},
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&NumberValue{0},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -359,19 +342,18 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Bytecode{
|
||||
InstructionConstant, 0,
|
||||
InstructionDeclareLocal, 1,
|
||||
InstructionPop,
|
||||
InstructionConstant, 2,
|
||||
InstructionSetLocal, 1, // reassign
|
||||
InstructionPop,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &StringValue{"a"}, &FloatValue{1},
|
||||
&NumberValue{0}, &StringValue{"a"}, &NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &FloatValue{1},
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&NumberValue{1},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -380,18 +362,19 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Bytecode{
|
||||
InstructionConstant, 0,
|
||||
InstructionDeclareLocal, 1,
|
||||
InstructionGetLocal, 1, // reassign
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &StringValue{"a"},
|
||||
&NumberValue{0}, &StringValue{"a"},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{0},
|
||||
},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &FloatValue{0},
|
||||
&VariableValue{
|
||||
"a",
|
||||
&NumberValue{0},
|
||||
0,
|
||||
},
|
||||
&NumberValue{0},
|
||||
},
|
||||
},
|
||||
"get_reassigned_local": {
|
||||
|
|
@ -399,25 +382,23 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Bytecode{
|
||||
InstructionConstant, 0,
|
||||
InstructionDeclareLocal, 1,
|
||||
InstructionPop,
|
||||
InstructionGetLocal, 1,
|
||||
InstructionConstant, 2,
|
||||
InstructionSetLocal, 1, // reassign
|
||||
InstructionPop,
|
||||
InstructionGetLocal, 1,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &StringValue{"a"}, &FloatValue{1},
|
||||
&NumberValue{0}, &StringValue{"a"}, &NumberValue{1},
|
||||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{0},
|
||||
&FloatValue{1},
|
||||
},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &FloatValue{1},
|
||||
&VariableValue{
|
||||
"a",
|
||||
&NumberValue{1},
|
||||
0,
|
||||
},
|
||||
&NumberValue{0},
|
||||
&NumberValue{1},
|
||||
},
|
||||
},
|
||||
"variable_scope": {
|
||||
|
|
@ -425,28 +406,26 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Bytecode{
|
||||
InstructionConstant, 0,
|
||||
InstructionDeclareLocal, 1,
|
||||
InstructionPop,
|
||||
InstructionDescend,
|
||||
InstructionConstant, 2,
|
||||
InstructionDeclareLocal, 3,
|
||||
InstructionPop,
|
||||
InstructionDescend,
|
||||
InstructionConstant, 4,
|
||||
InstructionDeclareLocal, 5,
|
||||
InstructionPop,
|
||||
InstructionAscend,
|
||||
InstructionAscend,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &StringValue{"a"},
|
||||
&FloatValue{1}, &StringValue{"b"},
|
||||
&FloatValue{2}, &StringValue{"c"},
|
||||
&NumberValue{0}, &StringValue{"a"},
|
||||
&NumberValue{1}, &StringValue{"b"},
|
||||
&NumberValue{2}, &StringValue{"c"},
|
||||
},
|
||||
),
|
||||
[]Value{},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &FloatValue{0},
|
||||
[]Value{
|
||||
&VariableValue{
|
||||
"a",
|
||||
&NumberValue{0},
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -459,25 +438,25 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionCall,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
&FunctionValue{
|
||||
Name: "sum",
|
||||
Params: []FunctionParameter{
|
||||
{
|
||||
"a",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
{
|
||||
"b",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
Chunk: NewChunk(
|
||||
[]Bytecode{
|
||||
InstructionGetLocal, 0,
|
||||
InstructionGetLocal, 1,
|
||||
InstructionAddFloat,
|
||||
InstructionAdd,
|
||||
InstructionReturn,
|
||||
},
|
||||
[]Value{
|
||||
|
|
@ -488,34 +467,32 @@ func GetExecutionTestData() map[string]struct {
|
|||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{3},
|
||||
&NumberValue{3},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"function_calling_function": {
|
||||
NewChunk(
|
||||
[]Bytecode{
|
||||
InstructionConstant, 3,
|
||||
InstructionDeclareLocal, 4,
|
||||
InstructionPop,
|
||||
InstructionConstant, 0,
|
||||
InstructionConstant, 1,
|
||||
InstructionConstant, 2,
|
||||
InstructionCall,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
&FunctionValue{
|
||||
Name: "sum",
|
||||
Params: []FunctionParameter{
|
||||
{
|
||||
"a",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
{
|
||||
"b",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
Chunk: NewChunk(
|
||||
|
|
@ -524,7 +501,7 @@ func GetExecutionTestData() map[string]struct {
|
|||
InstructionGetLocal, 2, InstructionCall, // square the number
|
||||
InstructionGetLocal, 1,
|
||||
InstructionGetLocal, 2, InstructionCall, // square the number
|
||||
InstructionAddFloat,
|
||||
InstructionAdd,
|
||||
InstructionReturn,
|
||||
},
|
||||
[]Value{
|
||||
|
|
@ -537,14 +514,14 @@ func GetExecutionTestData() map[string]struct {
|
|||
Params: []FunctionParameter{
|
||||
{
|
||||
"n",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
Chunk: NewChunk(
|
||||
[]Bytecode{
|
||||
InstructionGetLocal, 0,
|
||||
InstructionGetLocal, 0,
|
||||
InstructionMulFloat,
|
||||
InstructionMul,
|
||||
InstructionReturn,
|
||||
},
|
||||
[]Value{
|
||||
|
|
@ -556,23 +533,21 @@ func GetExecutionTestData() map[string]struct {
|
|||
},
|
||||
),
|
||||
[]Value{
|
||||
&FloatValue{5},
|
||||
},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"square": &FunctionValue{
|
||||
&VariableValue{
|
||||
"square",
|
||||
&FunctionValue{
|
||||
Name: "square",
|
||||
Params: []FunctionParameter{
|
||||
{
|
||||
"n",
|
||||
&FloatSignature{},
|
||||
&NumberSignature{},
|
||||
},
|
||||
},
|
||||
Chunk: NewChunk(
|
||||
[]Bytecode{
|
||||
InstructionGetLocal, 0,
|
||||
InstructionGetLocal, 0,
|
||||
InstructionMulFloat,
|
||||
InstructionMul,
|
||||
InstructionReturn,
|
||||
},
|
||||
[]Value{
|
||||
|
|
@ -580,7 +555,9 @@ func GetExecutionTestData() map[string]struct {
|
|||
},
|
||||
),
|
||||
},
|
||||
0,
|
||||
},
|
||||
&NumberValue{5},
|
||||
},
|
||||
},
|
||||
"list_concat": {
|
||||
|
|
@ -593,13 +570,13 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{
|
||||
&ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
},
|
||||
},
|
||||
&ListValue{
|
||||
[]Value{
|
||||
&FloatValue{3},
|
||||
&NumberValue{3},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -607,13 +584,12 @@ func GetExecutionTestData() map[string]struct {
|
|||
[]Value{
|
||||
&ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&FloatValue{3},
|
||||
&NumberValue{1},
|
||||
&NumberValue{2},
|
||||
&NumberValue{3},
|
||||
},
|
||||
},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -628,7 +604,7 @@ func TestVM_Execution(t *testing.T) {
|
|||
for vm.Next() {
|
||||
}
|
||||
|
||||
CompareStacks(t, test.resultingStack, vm.Stack)
|
||||
CompareStacks(t, test.resultingStack, vm.stack)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -654,7 +630,7 @@ func TestVM_NextByte(t *testing.T) {
|
|||
InstructionConstant, 0,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0},
|
||||
&NumberValue{0},
|
||||
},
|
||||
),
|
||||
16,
|
||||
|
|
@ -765,7 +741,7 @@ func TestVM_Jump(t *testing.T) {
|
|||
InstructionConstant, 2,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &FloatValue{1}, &FloatValue{2},
|
||||
&NumberValue{0}, &NumberValue{1}, &NumberValue{2},
|
||||
},
|
||||
),
|
||||
16,
|
||||
|
|
@ -790,7 +766,7 @@ func TestVM_JumpFalse(t *testing.T) {
|
|||
InstructionConstant, 2,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &FloatValue{1}, &FloatValue{2},
|
||||
&NumberValue{0}, &NumberValue{1}, &NumberValue{2},
|
||||
},
|
||||
),
|
||||
16,
|
||||
|
|
@ -816,7 +792,7 @@ func TestVM_DontJumpFalse(t *testing.T) {
|
|||
InstructionConstant, 2,
|
||||
},
|
||||
[]Value{
|
||||
&FloatValue{0}, &FloatValue{1}, &FloatValue{2},
|
||||
&NumberValue{0}, &NumberValue{1}, &NumberValue{2},
|
||||
},
|
||||
),
|
||||
16,
|
||||
|
|
|
|||
11
era3.ang
11
era3.ang
|
|
@ -1,11 +0,0 @@
|
|||
fn counter() -> (fn() -> int) {
|
||||
i := 0
|
||||
|
||||
fn() -> int { i = i + 1 }
|
||||
}
|
||||
|
||||
next := counter()
|
||||
|
||||
println(next())
|
||||
println(next())
|
||||
println(next())
|
||||
|
|
@ -7,19 +7,19 @@
|
|||
terms := 1000000
|
||||
|
||||
# The running sum of terms
|
||||
tot := 0.0
|
||||
tot := 0
|
||||
|
||||
n := 1
|
||||
while n <= terms {
|
||||
tot = tot + 1.0/float(n*n)
|
||||
tot = tot + 1 / (n*n)
|
||||
n = n + 1
|
||||
}
|
||||
|
||||
tot = tot * 6.0
|
||||
tot = tot * 6
|
||||
|
||||
# get the absolute value of a number
|
||||
fn abs(x: float) -> float {
|
||||
if x < 0.0 {
|
||||
func abs(x: number) number {
|
||||
if x < 0 {
|
||||
return -x
|
||||
}
|
||||
return x
|
||||
|
|
@ -30,19 +30,19 @@ fn abs(x: float) -> float {
|
|||
# see: https://en.wikipedia.org/wiki/Newton's_method
|
||||
# The required accuracy
|
||||
SQRT_ACC := 0.00000001
|
||||
fn sqrt(x: float) -> float {
|
||||
pg := 0.0 # previous guess
|
||||
g := 1.0 # current guess
|
||||
func sqrt(x: number) number {
|
||||
pg := 0 # previous guess
|
||||
g := 1 # current guess
|
||||
|
||||
while abs(pg - g) >= SQRT_ACC {
|
||||
pg = g
|
||||
g = (pg + x/pg)/2.0
|
||||
g = (pg + tot/pg)/2
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
pi := sqrt(tot)
|
||||
tot = sqrt(tot)
|
||||
|
||||
# output the result
|
||||
println(pi)
|
||||
write(str(tot))
|
||||
|
|
|
|||
|
|
@ -13,5 +13,3 @@ _slow_blink := CSI + "5m"
|
|||
_rapid_blink := CSI + "6m"
|
||||
_strike := CSI + "9m"
|
||||
_primary_font := CSI + "10m"
|
||||
|
||||
fn red(s: string) -> string {}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
fn map(list: [any], f: fn(any) -> any) -> [any] {
|
||||
func map(list: list[any], f: func(any)any) list[any] {
|
||||
out := []
|
||||
|
||||
i := 0
|
||||
|
|
|
|||
81
lib/math.ang
81
lib/math.ang
|
|
@ -7,7 +7,7 @@ E := 2.718281828459045235360287471352
|
|||
# Get the absolute value of a number. If x is negative, the returned
|
||||
# value is positive and equal to `-x`. If x is positive or zero, the
|
||||
# returned value is x.
|
||||
fn absf(x: float) -> float {
|
||||
func abs(x: number) number {
|
||||
# if the number is negative
|
||||
if x < 0 {
|
||||
# negate it so it's positive
|
||||
|
|
@ -17,23 +17,15 @@ fn absf(x: float) -> float {
|
|||
return x
|
||||
}
|
||||
|
||||
fn absi(n: int) -> int {
|
||||
if n < 0 {
|
||||
-n
|
||||
} else {
|
||||
n
|
||||
}
|
||||
}
|
||||
|
||||
DERIVE_DX := 0.00000001
|
||||
fn derive(f: fn(float) -> float, x: float) float {
|
||||
func derive(f: func(number)number, x: number) number {
|
||||
return (f(x + DERIVE_DX) - f(x))/DERIVE_DX
|
||||
}
|
||||
|
||||
NEWTONS_ACC := 0.000000000001
|
||||
fn newtons(f: fn(float) -> float) -> float {
|
||||
pg := 0.0
|
||||
g := 1.0
|
||||
func newtons(f: func(number)number) number {
|
||||
pg := 0
|
||||
g := 1
|
||||
|
||||
while abs(g - pg) > NEWTONS_ACC {
|
||||
pg = g
|
||||
|
|
@ -49,9 +41,9 @@ MAX_SQRT_DX := 0.0000001
|
|||
# x: number
|
||||
# Calculate the approximate square root using newton's method until
|
||||
# the accuracy has increased by less than the variable `MAX_SQRT_DX`.
|
||||
fn sqrt(x: float) -> float {
|
||||
func sqrt(x: number) number {
|
||||
ng := x
|
||||
g := 1.0
|
||||
g := 1
|
||||
|
||||
while abs(g - ng) > MAX_SQRT_DX {
|
||||
g = ng
|
||||
|
|
@ -59,6 +51,8 @@ fn sqrt(x: float) -> float {
|
|||
# create new guess
|
||||
ng = (g + x / g) / 2
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
# floor(x)
|
||||
|
|
@ -78,7 +72,7 @@ fn sqrt(x: float) -> float {
|
|||
# round(x)
|
||||
# x: number
|
||||
# Return the closest whole number to the value x.
|
||||
fn round(x: float) -> float {
|
||||
func round(x: number) number {
|
||||
f := floor(x)
|
||||
|
||||
if x - f > 0.5 {
|
||||
|
|
@ -92,7 +86,7 @@ fn round(x: float) -> float {
|
|||
# x: number; any number
|
||||
# n: number; the number to divide by
|
||||
# Return the rest from a division of x by n.
|
||||
fn mod(x: float, n: float) -> float {
|
||||
func mod(x: number, n: number) number {
|
||||
if x == 0 {
|
||||
return 0
|
||||
}
|
||||
|
|
@ -116,18 +110,17 @@ fn mod(x: float, n: float) -> float {
|
|||
# This value is only reasonable if 0<x<1.
|
||||
# It is approximated using the taylor series of e**x.
|
||||
SM_EXP_ACC := 0.00000000001
|
||||
fn sm_exp(x: float) -> float {
|
||||
p_tot := 0.0
|
||||
tot := 1.0
|
||||
func sm_exp(x: number) number {
|
||||
p_tot := 0
|
||||
tot := 1
|
||||
n := 1
|
||||
x_pow := x
|
||||
f := 1.0
|
||||
|
||||
f := 1
|
||||
while abs(tot - p_tot) > SM_EXP_ACC {
|
||||
p_tot = tot
|
||||
t := x_pow / f
|
||||
tot = tot + t
|
||||
f = f * float(n+1)
|
||||
f = f * (n+1)
|
||||
x_pow = x_pow * x
|
||||
n = n + 1
|
||||
}
|
||||
|
|
@ -138,22 +131,22 @@ fn sm_exp(x: float) -> float {
|
|||
# exp(x)
|
||||
# x: number; any number
|
||||
# Get an approximate value of e raised to the power of x.
|
||||
fn exp(x: float) -> float {
|
||||
func exp(x: number) number {
|
||||
n := abs(x)
|
||||
tot := 1.0
|
||||
tot := 1
|
||||
while n >= 1 {
|
||||
tot = tot * E
|
||||
n = n - 1
|
||||
}
|
||||
|
||||
if n > 0.0 {
|
||||
if n > 0 {
|
||||
tot = tot * sm_exp(n)
|
||||
}
|
||||
|
||||
if x < 0 {
|
||||
1.0/tot
|
||||
return 1/tot
|
||||
} else {
|
||||
tot
|
||||
return tot
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -162,9 +155,9 @@ fn exp(x: float) -> float {
|
|||
# Get the approximate value of the natural logarithm
|
||||
# This function uses newton's method to approximate.
|
||||
LN_ACC := 0.0000000001
|
||||
fn ln(x: float) -> float {
|
||||
pg := 0.0
|
||||
g := 1.0
|
||||
func ln(x: number) number {
|
||||
pg := 0
|
||||
g := 1
|
||||
|
||||
while abs(pg - g) > LN_ACC {
|
||||
pg = g
|
||||
|
|
@ -178,7 +171,7 @@ fn ln(x: float) -> float {
|
|||
# x: number; any number. The base
|
||||
# p: number; the value of the exponent
|
||||
# Raise any number to any power (x^p)
|
||||
fn pow(x: float, p: float) -> float {
|
||||
func pow(x: number, p: number) number {
|
||||
return exp(p*ln(x))
|
||||
}
|
||||
|
||||
|
|
@ -188,15 +181,15 @@ fn pow(x: float, p: float) -> float {
|
|||
# Calculate the approximate value of the logarithm
|
||||
# of a with b as base.
|
||||
LOG_ACC := 0.0000001
|
||||
fn log(a: float, b: float) -> float {
|
||||
func log(a: number, b: number) number {
|
||||
ln_b := ln(b)
|
||||
|
||||
pg := 0.0
|
||||
g := 1.0
|
||||
pg := 0
|
||||
g := 1
|
||||
|
||||
while abs(g - pg) > LOG_ACC {
|
||||
pg = g
|
||||
g = pg - 1.0/ln_b - a/(ln_b*pow(b, pg))
|
||||
g = pg - 1/ln_b - a/(ln_b*pow(b, pg))
|
||||
}
|
||||
|
||||
return g
|
||||
|
|
@ -206,9 +199,9 @@ fn log(a: float, b: float) -> float {
|
|||
# x: number; an angle in radians
|
||||
# Get the sine of an angle (in radians). https://en.wikipedia.org/wiki/Sine_and_cosine
|
||||
# TODO: use hashmap with precomputed values and linear interpolation
|
||||
fn sin(x: float) -> float {
|
||||
f := 1.0
|
||||
x = mod(x, 2.0*PI)
|
||||
func sin(x: number) number {
|
||||
f := 1
|
||||
x = mod(x, 2*PI)
|
||||
if x > PI {
|
||||
x = PI - x
|
||||
f = -1
|
||||
|
|
@ -216,13 +209,13 @@ fn sin(x: float) -> float {
|
|||
|
||||
# compute sine with a taylor series mock function of sine (valid between -pi and +pi)
|
||||
tot := x
|
||||
l := 1.0
|
||||
i := 1.0
|
||||
s := -1.0
|
||||
l := 1
|
||||
i := 1
|
||||
s := -1
|
||||
|
||||
while i <= 19 {
|
||||
i = i + 2.0
|
||||
l = s * l * x / i / (i-1.0)
|
||||
i = i + 2
|
||||
l = s * l * x / i / (i-1)
|
||||
|
||||
tot = tot + l
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
assertEq(1+1, 2)
|
||||
assertEq(3*2, 6)
|
||||
assertEq(3.0/4.0, 0.75)
|
||||
assertEq(3/4, 0.75)
|
||||
assertEq(2 - 5, -3)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ assertEq(1, 1)
|
|||
assertEq(0, 0)
|
||||
assertEq("", "")
|
||||
|
||||
assertEq([]int, []int)
|
||||
assertEq([]number, []number)
|
||||
assertEq([3, 1, 4, 1], [3, 1, 4, 1])
|
||||
|
||||
# Inequality
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
list := []int
|
||||
list := []number
|
||||
|
||||
x := 1
|
||||
while x <= 1000 {
|
||||
list.append(x)
|
||||
assertEq(list.reduce(fn(tot: int, a: int) -> int {
|
||||
assertEq(list.reduce(func(tot: number, a: number) number {
|
||||
return tot + a
|
||||
}, 0), x*(x + 1)/2)
|
||||
|
||||
x = x + 1
|
||||
}
|
||||
|
||||
fn sum(a: int, b: int) -> int {
|
||||
func sum(a: number, b: number) number {
|
||||
return a + b
|
||||
}
|
||||
|
||||
list = []int
|
||||
list = []number
|
||||
x = 1
|
||||
while x <= 100 {
|
||||
list.append(2*x - 1)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ fibonacci_numbers := [
|
|||
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
|
||||
]
|
||||
|
||||
fn fib(n: int) -> int {
|
||||
func fib(n: number) number {
|
||||
if n < 2 {
|
||||
return n
|
||||
}
|
||||
|
|
@ -29,4 +29,4 @@ while x < fibonacci_numbers.length() {
|
|||
x = x + 1
|
||||
}
|
||||
|
||||
println("")
|
||||
write("")
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
list := []int
|
||||
list := []number
|
||||
|
||||
list.append(1)
|
||||
list.append(2)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
|
||||
fn sum(a: int, b: int) -> int {
|
||||
func sum(a: number, b: number) number {
|
||||
return a + b
|
||||
}
|
||||
|
||||
breakpoint
|
||||
|
||||
assertEq(sum(1, 2), 3)
|
||||
breakpoint
|
||||
|
||||
assertEq(sum(3, 3), 6)
|
||||
breakpoint
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
|
||||
assertEq(type(1), "int")
|
||||
assertEq(type(1), "number")
|
||||
assertEq(type("Hello"), "string")
|
||||
assertEq(type(true), "boolean")
|
||||
|
||||
# lists
|
||||
assertEq(type(["Hello", "world"]), "list[string]")
|
||||
assertEq(type([0, 1]), "list[int]")
|
||||
assertEq(type([0, 1]), "list[number]")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue