From 7996420ee56f978df4a75423c2e73dd4467e5387 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 6 Dec 2018 15:32:07 +0100 Subject: [PATCH] Fixes for require --- .../ScriptFunction/ScriptMethodInfoFunction.cs | 10 ++++++---- Upsilon/BaseTypes/UserData/UserDataMethod.cs | 13 ++++++++++++- Upsilon/Binder/BoundBinaryOperator.cs | 14 +++++++++++++- .../InternalFunctionVariableSymbol.cs | 2 +- Upsilon/Evaluator/Evaluator.cs | 4 ++++ Upsilon/Executor.cs | 2 +- Upsilon/StandardLibraries/BasicFunctions.cs | 3 ++- 7 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Upsilon/BaseTypes/ScriptFunction/ScriptMethodInfoFunction.cs b/Upsilon/BaseTypes/ScriptFunction/ScriptMethodInfoFunction.cs index 06f265f..dc8039a 100644 --- a/Upsilon/BaseTypes/ScriptFunction/ScriptMethodInfoFunction.cs +++ b/Upsilon/BaseTypes/ScriptFunction/ScriptMethodInfoFunction.cs @@ -62,18 +62,20 @@ namespace Upsilon.BaseTypes.ScriptFunction } } + object result; try { - var result = method.Invoke(_object, objects.ToArray()); - if (_directTypeManipulation) - return (ScriptType)result; - return result.ToScriptType(); + result = method.Invoke(_object, objects.ToArray()); } catch (TargetInvocationException e) { if (e.InnerException != null) throw e.InnerException; throw; } + if (_directTypeManipulation) + return (ScriptType)result; + return result.ToScriptType(); + } } } \ No newline at end of file diff --git a/Upsilon/BaseTypes/UserData/UserDataMethod.cs b/Upsilon/BaseTypes/UserData/UserDataMethod.cs index 2d93c02..fcb864a 100644 --- a/Upsilon/BaseTypes/UserData/UserDataMethod.cs +++ b/Upsilon/BaseTypes/UserData/UserDataMethod.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Upsilon.Evaluator; namespace Upsilon.BaseTypes.UserData { @@ -11,7 +12,17 @@ namespace Upsilon.BaseTypes.UserData public UserDataMethodPart(MethodInfo method) { Method = method; - Parameters = method.GetParameters().Select(x => new UserDataMethodParameter(x)).ToArray(); + var pars = method.GetParameters(); + var ls = new List(); + foreach (var parameter in pars) + { + if (parameter.ParameterType == typeof(Script)) + continue; + if (parameter.ParameterType == typeof(EvaluationScope)) + continue; + ls.Add(new UserDataMethodParameter(parameter)); + } + Parameters = ls.ToArray(); } public MethodInfo Method { get; } diff --git a/Upsilon/Binder/BoundBinaryOperator.cs b/Upsilon/Binder/BoundBinaryOperator.cs index f83cf75..5fb1574 100644 --- a/Upsilon/Binder/BoundBinaryOperator.cs +++ b/Upsilon/Binder/BoundBinaryOperator.cs @@ -13,7 +13,9 @@ namespace Upsilon.Binder GreaterEquals, Greater, LessEquals, - Less + Less, + And, + Or } private Type LeftType { get; } @@ -59,6 +61,10 @@ namespace Upsilon.Binder new BoundBinaryOperator(OperatorKind.Equality, Type.Boolean), new BoundBinaryOperator(OperatorKind.Inequality, Type.Boolean), + // Boolean chaining + new BoundBinaryOperator(OperatorKind.And, Type.Boolean), + new BoundBinaryOperator(OperatorKind.Or, Type.Boolean), + // String operators new BoundBinaryOperator(OperatorKind.Addition, Type.String, Type.String, Type.String), new BoundBinaryOperator(OperatorKind.Addition, Type.String, Type.Number, Type.String), @@ -114,6 +120,12 @@ namespace Upsilon.Binder case SyntaxKind.GreaterEquals: kind = OperatorKind.GreaterEquals; break; + case SyntaxKind.AndKeyword: + kind = OperatorKind.And; + break; + case SyntaxKind.OrKeyword: + kind = OperatorKind.Or; + break; default: throw new Exception("Unknown binary operator token: " + operatorToken); } diff --git a/Upsilon/Binder/VariableSymbols/InternalFunctionVariableSymbol.cs b/Upsilon/Binder/VariableSymbols/InternalFunctionVariableSymbol.cs index fac4d04..fbebcb9 100644 --- a/Upsilon/Binder/VariableSymbols/InternalFunctionVariableSymbol.cs +++ b/Upsilon/Binder/VariableSymbols/InternalFunctionVariableSymbol.cs @@ -50,7 +50,7 @@ namespace Upsilon.Binder.VariableSymbols if (!functionParameter.ValidTypes.HasFlag(callingParameter.Type)) { return (false, - $"Unexpected variable passed to internal function at position {i + 1}. " + + $"Unexpected variable passed to internal function at variable {i + 1}. " + $"Expected one of the following: {functionParameter.ValidTypes.ToString()}, got: '{callingParameter.Type}'", callingParameter); } diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index e8da4c4..e11defe 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -359,6 +359,10 @@ namespace Upsilon.Evaluator ThrowException($"Can't find operator for types '{left.Type}' and '{right.Type}'", e.Span); return new ScriptNull(); + case BoundBinaryOperator.OperatorKind.And: + return new ScriptBoolean((ScriptBoolean) left && (ScriptBoolean) right); + case BoundBinaryOperator.OperatorKind.Or: + return new ScriptBoolean((ScriptBoolean) left || (ScriptBoolean) right); default: throw new Exception("Invalid Binary Operator: " + e.Operator); } diff --git a/Upsilon/Executor.cs b/Upsilon/Executor.cs index 325bfe1..f778d76 100644 --- a/Upsilon/Executor.cs +++ b/Upsilon/Executor.cs @@ -53,7 +53,7 @@ namespace Upsilon public static Script ContinueWith(Script script, BoundScript bound) { var s = Script.ContinueWith(script, bound); - s.Parse(); + s.Evaluate(); return s; } diff --git a/Upsilon/StandardLibraries/BasicFunctions.cs b/Upsilon/StandardLibraries/BasicFunctions.cs index cb7e7d1..960dd2e 100644 --- a/Upsilon/StandardLibraries/BasicFunctions.cs +++ b/Upsilon/StandardLibraries/BasicFunctions.cs @@ -45,8 +45,9 @@ namespace Upsilon.StandardLibraries [StandardLibraryScriptFunction("require", "Loads a module from the module path, using the given script loader.", true)] - public ScriptType Require(Script script, string file) + public ScriptType Require(Script script, ScriptString fileName) { + var file = fileName.Value; var loader = script.Options.ScriptLoader; if (loader != null) {