diff --git a/Makefile b/Makefile index 622fb51..326f5be 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,11 @@ it: @ go build -o ${BINARY_NAME} ./cmd/lambda @ chmod +x ${BINARY_NAME} -ex: it +simple: it @ ./lambda.exe - < ./samples/simple.txt -v: it - @ ./lambda.exe -v - < ./samples/simple.txt \ No newline at end of file +thunk: it + @ ./lambda.exe - < ./samples/thunk.txt + +saccharine: it + @ ./lambda.exe - < ./samples/saccharine.txt \ No newline at end of file diff --git a/pkg/saccharine/ast/node.go b/pkg/saccharine/ast/expression.go similarity index 79% rename from pkg/saccharine/ast/node.go rename to pkg/saccharine/ast/expression.go index c86d83d..9c04c59 100644 --- a/pkg/saccharine/ast/node.go +++ b/pkg/saccharine/ast/expression.go @@ -20,6 +20,10 @@ type Atom struct { Name string } +func (Abstraction) IsExpression() {} +func (Application) IsExpression() {} +func (Atom) IsExpression() {} + /** ------------------------------------------------------------------------- */ func NewAbstraction(parameter []string, body Expression) *Abstraction { @@ -33,9 +37,3 @@ func NewApplication(abstraction Expression, arguments []Expression) *Application func NewAtom(name string) *Atom { return &Atom{Name: name} } - -/** ------------------------------------------------------------------------- */ - -func (a Abstraction) IsExpression() {} -func (a Application) IsExpression() {} -func (v Atom) IsExpression() {} diff --git a/pkg/saccharine/ast/program.go b/pkg/saccharine/ast/program.go new file mode 100644 index 0000000..f7e131f --- /dev/null +++ b/pkg/saccharine/ast/program.go @@ -0,0 +1,5 @@ +package ast + +type Program struct { + Statements []Statement +} diff --git a/pkg/saccharine/ast/statement.go b/pkg/saccharine/ast/statement.go new file mode 100644 index 0000000..4a142f3 --- /dev/null +++ b/pkg/saccharine/ast/statement.go @@ -0,0 +1,28 @@ +package ast + +type Statement interface { + IsStatement() +} + +/** ------------------------------------------------------------------------- */ + +type LetStatement struct { + Variable string + Value Expression +} + +type MethodStatement struct { + Name string + Parameters []string + Body Expression +} + +type DeclareStatement struct { + Value Expression +} + +func (LetStatement) IsStatement() {} +func (MethodStatement) IsStatement() {} +func (DeclareStatement) IsStatement() {} + +/** ------------------------------------------------------------------------- */ diff --git a/pkg/saccharine/tokenizer.go b/pkg/saccharine/tokenizer.go index f2a156f..4968b6e 100644 --- a/pkg/saccharine/tokenizer.go +++ b/pkg/saccharine/tokenizer.go @@ -69,7 +69,7 @@ func getToken(i *iterator.Iterator[rune]) (*token.Token, error) { return token.NewAtom(string(atom), index), nil } - return nil, fmt.Errorf("unknown character '%v'", letter) + return nil, fmt.Errorf("unknown character '%v'", string(letter)) } // Parses a list of runes into tokens. All error encountered are returned, as well. diff --git a/samples/saccharine.txt b/samples/saccharine.txt index b1f35b1..c002c11 100644 --- a/samples/saccharine.txt +++ b/samples/saccharine.txt @@ -1,9 +1,9 @@ 0 := \f x.x -inc n := \f x.f (n f x) -add n m := m inc n -mult n m := m (n f) -exp m n := m n +(inc n) := \f x.(f (n f x)) +(add n m) := (m inc n) +(mult n m) := (m (n f)) +(exp m n) := (m n) # This is the final output. 5 := (inc (inc (inc (inc (inc 0))))) -exp 5 5 +(exp 5 5) diff --git a/samples/thunk.txt b/samples/thunk.txt new file mode 100644 index 0000000..4181291 --- /dev/null +++ b/samples/thunk.txt @@ -0,0 +1 @@ +(\.VALUE anything) \ No newline at end of file