-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7474e1f
commit d29f495
Showing
8 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package evaluator | ||
|
||
import ( | ||
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/ast" | ||
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/object" | ||
) | ||
|
||
func DefineMacros(program *ast.Program, env *object.Environment) { | ||
definitions := []int{} | ||
for i, statement := range program.Statements { | ||
if isMacroDefinition(statement) { | ||
addMacro(statement, env) | ||
definitions = append(definitions, i) | ||
} | ||
} | ||
for i := len(definitions) - 1; i >= 0; i = i - 1 { | ||
definitionIndex := definitions[i] | ||
program.Statements = append( | ||
program.Statements[:definitionIndex], | ||
program.Statements[definitionIndex+1:]..., | ||
) | ||
} | ||
} | ||
|
||
func isMacroDefinition(node ast.Statement) bool { | ||
assignStatement, ok := node.(*ast.AssignStatement) | ||
if !ok { | ||
return false | ||
} | ||
_, ok = assignStatement.Value.(*ast.MacroLiteral) | ||
return ok | ||
} | ||
|
||
func addMacro(stmt ast.Statement, env *object.Environment) { | ||
assignStatement, _ := stmt.(*ast.AssignStatement) | ||
macroLiteral, _ := assignStatement.Value.(*ast.MacroLiteral) | ||
macro := &object.Macro{ | ||
Parameters: macroLiteral.Parameters, | ||
Env: env, | ||
Body: macroLiteral.Body, | ||
} | ||
env.Set(assignStatement.Name.Value, macro) | ||
} | ||
|
||
func ExpandMacros(program ast.Node, env *object.Environment) ast.Node { | ||
return ast.Modify(program, func(node ast.Node) ast.Node { | ||
callExpression, ok := node.(*ast.CallExpression) | ||
if !ok { | ||
return node | ||
} | ||
macro, ok := isMacroCall(callExpression, env) | ||
if !ok { | ||
return node | ||
} | ||
args := quoteArgs(callExpression) | ||
evalEnv := extendMacroEnv(macro, args) | ||
evaluated := Eval(macro.Body, evalEnv) | ||
quote, ok := evaluated.(*object.Quote) | ||
if !ok { | ||
panic("we only support returning AST-nodes from macros") | ||
} | ||
return quote.Node | ||
}) | ||
} | ||
func isMacroCall( | ||
exp *ast.CallExpression, | ||
env *object.Environment, | ||
) (*object.Macro, bool) { | ||
identifier, ok := exp.Function.(*ast.Identifier) | ||
if !ok { | ||
return nil, false | ||
} | ||
obj, ok := env.Get(identifier.Value) | ||
if !ok { | ||
return nil, false | ||
} | ||
macro, ok := obj.(*object.Macro) | ||
if !ok { | ||
return nil, false | ||
} | ||
return macro, true | ||
} | ||
|
||
func quoteArgs(exp *ast.CallExpression) []*object.Quote { | ||
args := []*object.Quote{} | ||
for _, a := range exp.Arguments { | ||
args = append(args, &object.Quote{Node: a}) | ||
} | ||
return args | ||
} | ||
|
||
func extendMacroEnv( | ||
macro *object.Macro, | ||
args []*object.Quote, | ||
) *object.Environment { | ||
extended := object.NewEnclosedEnvironment(macro.Env) | ||
for paramIdx, param := range macro.Parameters { | ||
extended.Set(param.Value, args[paramIdx]) | ||
} | ||
return extended | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var unless = macro(condition, consequence, alternative) { quote(if (!(unquote(condition))) { unquote(consequence); } else { unquote(alternative); }); }; | ||
|
||
unless(10 > 5, println("not greater"), println("greater")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters