Implement evaluation of method calls

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-11-27 23:28:16 -08:00
parent b299c73824
commit 24650f0db6
4 changed files with 40 additions and 8 deletions

View File

@@ -11,6 +11,9 @@ namespace Orchard.Widgets.SimpleScripting.Ast {
_arguments = arguments;
}
public Token Target { get { return _token; } }
public IList<AstNode> Arguments { get { return _arguments; } }
public Token Token { get { return _token; } }
public override IEnumerable<AstNode> Children {

View File

@@ -4,16 +4,14 @@ using Orchard.Widgets.SimpleScripting.Ast;
namespace Orchard.Widgets.SimpleScripting.Compiler {
public class Interpreter {
private readonly InterpreterVisitor _interpreterVisitor = new InterpreterVisitor();
public EvaluationResult Evalutate(EvaluationContext context) {
return _interpreterVisitor.Evaluate(context);
return new InterpreterVisitor(context).Evaluate();
}
}
public class EvaluationContext {
public AbstractSyntaxTree Tree { get; set; }
public Func<object, string, IList<object>> MethodInvocationCallback { get; set; }
public Func<string, IList<object>, object> MethodInvocationCallback { get; set; }
}
public class EvaluationResult {

View File

@@ -1,10 +1,17 @@
using System;
using System.Linq;
using Orchard.Widgets.SimpleScripting.Ast;
namespace Orchard.Widgets.SimpleScripting.Compiler {
public class InterpreterVisitor : AstVisitor {
public EvaluationResult Evaluate(EvaluationContext context) {
return Evaluate(context.Tree.Root);
private readonly EvaluationContext _context;
public InterpreterVisitor(EvaluationContext context) {
_context = context;
}
public EvaluationResult Evaluate() {
return Evaluate(_context.Tree.Root);
}
private EvaluationResult Evaluate(AstNode node) {
@@ -54,6 +61,14 @@ namespace Orchard.Widgets.SimpleScripting.Compiler {
}
}
public override object VisitMethodCall(MethodCallAstNode node) {
var arguments = node.Arguments.Select(arg => Evaluate(arg)).ToList();
if (arguments.Any(arg => arg.IsError))
return arguments.First(arg => arg.IsError);
return Result(_context.MethodInvocationCallback((string)node.Target.Value, arguments.Select(arg => arg.Value).ToList()));
}
public override object VisitError(ErrorAstNode node) {
return Error(node.Message);
}