Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/MoonSharp.Interpreter/Execution/Scopes/BuildTimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public SymbolRef TryDefineLocal(string name)
return m_Frames.Last().TryDefineLocal(name);
}

public void BlockResolution(IEnumerable<SymbolRef> locals)
{
m_Frames.Last().BlockResolution(locals);
}

public void UnblockResolution() => m_Frames.Last().UnblockResolution();


public bool CurrentFunctionHasVarArgs()
{
return m_Frames.Last().HasVarArgs;
Expand Down
17 changes: 17 additions & 0 deletions src/MoonSharp.Interpreter/Execution/Scopes/BuildTimeScopeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,25 @@ internal BuildTimeScopeBlock AddChild()
return block;
}

private HashSet<string> blockedNames = new HashSet<string>();
internal void BlockResolution(IEnumerable<SymbolRef> locals)
{
foreach(var l in locals) {
if (!m_DefinedNames.ContainsValue(l))
throw new InternalErrorException("Tried to block resolution of local outside of block");
blockedNames.Add(l.Name);
}
}

internal void UnblockResolution()
{
blockedNames.Clear();
}


internal SymbolRef Find(string name)
{
if (blockedNames.Contains(name)) return null;
return m_DefinedNames.GetOrDefault(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ internal SymbolRef DefineLocal(string name)
return m_ScopeTreeHead.Define(name);
}

internal void BlockResolution(IEnumerable<SymbolRef> locals)
{
m_ScopeTreeHead.BlockResolution(locals);
}

internal void UnblockResolution() => m_ScopeTreeHead.UnblockResolution();

internal SymbolRef TryDefineLocal(string name)
{
if (m_ScopeTreeHead.Find(name) != null)
Expand Down
2 changes: 2 additions & 0 deletions src/MoonSharp.Interpreter/Tree/Expression_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public virtual string GetFriendlyDebugName()

public abstract DynValue Eval(ScriptExecutionContext context);

public abstract void ResolveScope(ScriptLoadingContext lcontext);

public abstract bool EvalLiteral(out DynValue dv);

public void CompilePossibleLiteral(ByteCode bc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public override DynValue Eval(ScriptExecutionContext context)
return expression.Eval(context).ToScalar();
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
expression.ResolveScope(lcontext);
}

public override bool EvalLiteral(out DynValue dv)
{
if (expression.EvalLiteral(out dv))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ public static OpCode OperatorToOpCode(Operator op)
}
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
m_Exp1.ResolveScope(lcontext);
m_Exp2.ResolveScope(lcontext);
}


public override void Compile(Execution.VM.ByteCode bc)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public DynamicExprExpression(Expression exp, ScriptLoadingContext lcontext)
m_Exp = exp;
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
m_Exp.ResolveScope(lcontext);
}

public override DynValue Eval(ScriptExecutionContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public Expression[] GetExpressions()
return expressions.ToArray();
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
foreach(var exp in expressions)
exp.ResolveScope(lcontext);
}

public override void Compile(Execution.VM.ByteCode bc)
{
foreach (var exp in expressions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public FunctionCallExpression(ScriptLoadingContext lcontext, Expression function
}
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
m_Function.ResolveScope(lcontext);
foreach(var arg in m_Arguments)
arg.ResolveScope(lcontext);
}

public override void Compile(Execution.VM.ByteCode bc)
{
m_Function.Compile(bc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,7 @@ private FunctionDefinitionExpression(ScriptLoadingContext lcontext, bool pushSel

m_Annotations = lcontext.FunctionAnnotations.ToArray();
lcontext.FunctionAnnotations = new List<Annotation>();

// create scope
// This needs to be up here to allow for arguments to correctly close over ENV
// Arguments, however, must come before any other local definitions to avoid closing
// over uninitialised variables. (Note for hoisting).
lcontext.Scope.PushFunction(this);

if (m_UsesGlobalEnv)
{
m_Env = lcontext.Scope.DefineLocal(WellKnownSymbols.ENV);
}
else
{
lcontext.Scope.ForceEnvUpValue();
}


// Parse arguments
// here lexer should be at the '(' or at the '|'
//Token openRound = CheckTokenType(lcontext, isLambda ? TokenType.Lambda : TokenType.Brk_Open_Round);
Expand Down Expand Up @@ -97,24 +82,41 @@ private FunctionDefinitionExpression(ScriptLoadingContext lcontext, bool pushSel
// here lexer is at first token of body

m_Begin = openRound.GetSourceRefUpTo(lcontext.Lexer.Current);


m_ParamNames = DefineArguments(paramnames, lcontext);

if(m_HasVarArgs) lcontext.Scope.SetHasVarArgs(); //Moved here

if(isLambda)
m_Statement = CreateLambdaBody(lcontext, arrowFunc);
else
m_Statement = CreateBody(lcontext, openCurly);

m_StackFrame = lcontext.Scope.PopFunction();

lcontext.Source.Refs.Add(m_Begin);
lcontext.Source.Refs.Add(m_End);

}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
resolved = true;
lcontext.Scope.PushFunction(this);

if (m_UsesGlobalEnv)
{
m_Env = lcontext.Scope.DefineLocal(WellKnownSymbols.ENV);
}
else
{
lcontext.Scope.ForceEnvUpValue();
}

m_ParamNames = DefineArguments(paramnames, lcontext);

if(m_HasVarArgs) lcontext.Scope.SetHasVarArgs(); //Moved here

m_Statement.ResolveScope(lcontext);

m_StackFrame = lcontext.Scope.PopFunction();
}


private Statement CreateLambdaBody(ScriptLoadingContext lcontext, bool arrowFunc)
{
Expand Down Expand Up @@ -247,7 +249,7 @@ private SymbolRef[] DefineArguments(List<FunctionDefinitionStatement.FunctionPar
{
if (!names.Add(paramnames[i].Name))
paramnames[i].Name = paramnames[i].Name + "@" + i.ToString();

paramnames[i].DefaultValue?.ResolveScope(lcontext);
ret[i] = lcontext.Scope.DefineLocal(paramnames[i].Name);
}

Expand Down Expand Up @@ -281,8 +283,11 @@ public override DynValue Eval(ScriptExecutionContext context)
throw new DynamicExpressionException("Dynamic Expressions cannot define new functions.");
}

private bool resolved = false;

public int CompileBody(ByteCode bc, string friendlyName)
{
if (!resolved) throw new InternalErrorException("Function definition scope not resolved");
//LoadingContext.Scope.PopFunction()

string funcName = friendlyName ?? ("<" + this.m_Begin.FormatLocation(bc.Script, true) + ">");
Expand Down
6 changes: 6 additions & 0 deletions src/MoonSharp.Interpreter/Tree/Expressions/IndexExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public IndexExpression(Expression baseExp, Token nameToken, bool nilCheck, Scrip
}


public override void ResolveScope(ScriptLoadingContext lcontext)
{
m_BaseExp.ResolveScope(lcontext);
m_IndexExp?.ResolveScope(lcontext);
}

public override void Compile(ByteCode bc)
{
m_BaseExp.Compile(bc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public LiteralExpression(ScriptLoadingContext lcontext, Token t)
lcontext.Lexer.Next();
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
//No-op
}

public override void Compile(Execution.VM.ByteCode bc)
{
bc.Emit_Literal(m_Value);
Expand Down
41 changes: 24 additions & 17 deletions src/MoonSharp.Interpreter/Tree/Expressions/SymbolRefExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class SymbolRefExpression : Expression, IVariable
{
SymbolRef m_Ref;
string m_VarName;

private Token T;

private bool inc = false;
private bool dec = false;

Expand All @@ -20,22 +21,7 @@ public SymbolRefExpression(Token T, ScriptLoadingContext lcontext)
: base(lcontext)
{
m_VarName = T.Text;

if (T.Type == TokenType.VarArgs)
{
m_Ref = lcontext.Scope.Find(WellKnownSymbols.VARARGS);

if (!lcontext.Scope.CurrentFunctionHasVarArgs())
throw new SyntaxErrorException(T, "cannot use '...' outside a vararg function");

if (lcontext.IsDynamicExpression)
throw new DynamicExpressionException("cannot use '...' in a dynamic expression.");
}
else
{
if (!lcontext.IsDynamicExpression)
m_Ref = lcontext.Scope.Find(m_VarName);
}
this.T = T;

lcontext.Lexer.Next();
//inc/dec expr
Expand All @@ -51,6 +37,27 @@ public SymbolRefExpression(Token T, ScriptLoadingContext lcontext)
}
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
if (m_Ref == null) {
if (T.Type == TokenType.VarArgs)
{
m_Ref = lcontext.Scope.Find(WellKnownSymbols.VARARGS);

if (!lcontext.Scope.CurrentFunctionHasVarArgs())
throw new SyntaxErrorException(T, "cannot use '...' outside a vararg function");

if (lcontext.IsDynamicExpression)
throw new DynamicExpressionException("cannot use '...' in a dynamic expression.");
}
else
{
if (!lcontext.IsDynamicExpression)
m_Ref = lcontext.Scope.Find(m_VarName);
}
}
}

public SymbolRefExpression(ScriptLoadingContext lcontext, SymbolRef refr)
: base(lcontext)
{
Expand Down
13 changes: 13 additions & 0 deletions src/MoonSharp.Interpreter/Tree/Expressions/TableConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ public bool TryGetLiteral(out DynValue dv)
return true;
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
foreach (var kvp in m_CtorArgs)
{
kvp.Key.ResolveScope(lcontext);
kvp.Value.ResolveScope(lcontext);
}
foreach (var p in m_PositionalValues)
{
p.ResolveScope(lcontext);
}
}


public override void Compile(Execution.VM.ByteCode bc)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public override void Compile(ByteCode bc)
bc.Emit_StrFormat(arguments.Count);
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
foreach(var exp in arguments)
exp.ResolveScope(lcontext);
}

public override DynValue Eval(ScriptExecutionContext context)
{
return DynValue.NewString(string.Format(formatString, arguments.Select(x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public override void Compile(ByteCode bc)
}
}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
condition.ResolveScope(lcontext);
exp1.ResolveScope(lcontext);
exp2.ResolveScope(lcontext);
}

public override DynValue Eval(ScriptExecutionContext context)
{
if (condition.Eval(context).CastToBool())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public override void Compile(ByteCode bc)

}

public override void ResolveScope(ScriptLoadingContext lcontext)
{
m_Exp.ResolveScope(lcontext);
}

public override DynValue Eval(ScriptExecutionContext context)
{
DynValue v = m_Exp.Eval(context).ToScalar();
Expand Down
Loading