-
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
68ebf64
commit a4ae9fd
Showing
6 changed files
with
213 additions
and
3 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
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,44 @@ | ||
package evaluator | ||
|
||
import ( | ||
"github.com/grantwforsythe/monkeylang/pkg/ast" | ||
"github.com/grantwforsythe/monkeylang/pkg/object" | ||
) | ||
|
||
func quote(node ast.Node, env *object.Environment) object.Object { | ||
return &object.Quote{Node: evalUnquoteCalls(node, env)} | ||
} | ||
|
||
func evalUnquoteCalls(quote ast.Node, env *object.Environment) ast.Node { | ||
return ast.Modify(quote, func(node ast.Node) ast.Node { | ||
if !isUnquoteCall(node) { | ||
return node | ||
} | ||
|
||
// We make this same assertion in isUnquoteCall so no need to do it again | ||
call, _ := node.(*ast.CallExpression) | ||
|
||
// Can only handle one expression | ||
if len(call.Arguments) != 1 { | ||
return node | ||
} | ||
|
||
eval := Eval(call.Arguments[0], env) | ||
|
||
convertible, ok := eval.(object.Convertible) | ||
if !ok { | ||
return node | ||
} | ||
|
||
return convertible.ToNode() | ||
}) | ||
} | ||
|
||
// Check if a node is a call expression for unquote. | ||
func isUnquoteCall(node ast.Node) bool { | ||
fn, ok := node.(*ast.CallExpression) | ||
if !ok { | ||
return false | ||
} | ||
return fn.Function.TokenLiteral() == "unquote" | ||
} |
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