feat: expression

This commit is contained in:
2025-12-27 03:43:19 -05:00
parent bf0edfc593
commit 0e185fbf41
7 changed files with 50 additions and 15 deletions

View File

@@ -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
thunk: it
@ ./lambda.exe - < ./samples/thunk.txt
saccharine: it
@ ./lambda.exe - < ./samples/saccharine.txt

View File

@@ -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() {}

View File

@@ -0,0 +1,5 @@
package ast
type Program struct {
Statements []Statement
}

View File

@@ -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() {}
/** ------------------------------------------------------------------------- */

View File

@@ -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.

View File

@@ -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)

1
samples/thunk.txt Normal file
View File

@@ -0,0 +1 @@
(\.VALUE anything)