Skip to content

Commit

Permalink
use return keyword for function return calls
Browse files Browse the repository at this point in the history
  • Loading branch information
manosriram committed Jul 13, 2024
1 parent 29f3225 commit 3d07e9d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pkg/ast/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func (e *Evaluator) Visit(node types.Node) interface{} {
case types.Function:
e.SymbolTable[n.Name] = n
case types.FunctionCall:
_, ok := e.SymbolTable[n.Name]
if !ok {
log.Fatalf("variable '%s' not defined", n.Name)
}
for _, v := range e.SymbolTable[n.Name].(types.Function).Children {
e.Visit(v)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func Tokenize(source string) *Source {
s.Tokens = append(s.Tokens, types.Token{TokenType: types.IF, Value: types.Literal{Value: "if", Type: types.STATEMENT}, LineNumber: s.CurrentLineNumber})
case "fn":
s.Tokens = append(s.Tokens, types.Token{TokenType: types.FUNCTION, Value: types.Literal{Value: "fn", Type: types.STATEMENT}, LineNumber: s.CurrentLineNumber})
case "ret":
case "return":
s.Tokens = append(s.Tokens, types.Token{TokenType: types.FUNCTION_RETURN, Value: types.Literal{Value: "return", Type: types.STATEMENT}, LineNumber: s.CurrentLineNumber})
default:
switch s.getCurrentToken() {
Expand Down
8 changes: 3 additions & 5 deletions source.lark
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ tt <- !false;
c <-> e;
a <-> c;

fn hi[] <<
a <- 1000;
bbb <- 123;
ret a+bbb;
fn add[] <<
return 1000+500;
>>

fnval <- hi();
fnval <- add();
2 changes: 1 addition & 1 deletion test_source_files/function.lark
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn addstatic[] <<
fna <- 1000;
fnb <- 500;
ret fna+fnb;
return fna+fnb;
>>

fnval <- addstatic();

0 comments on commit 3d07e9d

Please sign in to comment.