public override int Compile(FunctionContext context) { context.Line(FileName, Line); var start = context.MakeLabel("forStart"); var increment = context.MakeLabel("forContinue"); var end = context.MakeLabel("forEnd"); if (Initializer != null) CompileCheck(context, Initializer, 0); context.Bind(start); if (Condition != null) CompileCheck(context, Condition, 1); context.JumpFalse(end); context.PushLoop(increment, end); CompileCheck(context, Block, 0); context.PopLoop(); context.Bind(increment); if (Increment != null) CompileCheck(context, Increment, 0); context.Jump(start); context.Bind(end); return 0; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var identifier = context.Identifier(Name); if (!context.Compiler.Options.UseImplicitGlobals && identifier == null) { throw new MondCompilerException(FileName, Line, CompilerError.UndefinedIdentifier, Name); } if (identifier == null) { stack += context.LoadGlobal(); stack += context.LoadField(context.String(Name)); } else { stack += context.Load(identifier); } CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); context.LoadNull(); return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var start = context.MakeLabel("whileStart"); var end = context.MakeLabel("whileEnd"); var boolExpression = Condition as BoolExpression; var isInfinite = boolExpression != null && boolExpression.Value; stack += context.Bind(start); if (!isInfinite) { stack += Condition.Compile(context); stack += context.JumpFalse(end); } context.PushLoop(start, end); stack += Block.Compile(context); context.PopLoop(); stack += context.Jump(start); stack += context.Bind(end); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var sequenceContext = context as SequenceBodyContext; if (sequenceContext == null) { throw new MondCompilerException(FileName, Line, CompilerError.YieldInFun); } var state = sequenceContext.SequenceBody.State; var enumerable = sequenceContext.SequenceBody.Enumerable; var stack = 0; var nextState = sequenceContext.SequenceBody.NextState; var nextStateLabel = sequenceContext.SequenceBody.MakeStateLabel(context); stack += context.Load(context.Number(nextState)); stack += context.Store(state); stack += Value.Compile(context); stack += context.Load(enumerable); stack += context.StoreField(context.String("current")); stack += context.LoadTrue(); stack += context.Return(); stack += context.Bind(nextStateLabel); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); if (context.Name != null) { var callExpression = Value as CallExpression; if (callExpression != null) { var identifierExpression = callExpression.Method as IdentifierExpression; if (identifierExpression != null && context.Identifier(identifierExpression.Name) == context.Name) { callExpression.CompileTailCall(context); return 0; } } } if (Value != null) CompileCheck(context, Value, 1); else context.LoadUndefined(); context.Return(); return 0; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); CompileCheck(context, Left, 1); context.LoadField(context.String(Name)); return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); CompileCheck(context, Left, 1); CompileCheck(context, Index, 1); context.LoadArray(); return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var isStatement = Parent is IBlockStatementExpression; if (Name == null && isStatement) throw new MondCompilerException(FileName, Line, "Function is never used"); IdentifierOperand identifier = null; if (Name != null) { if (!context.DefineIdentifier(Name, true)) throw new MondCompilerException(FileName, Line, "Identifier '{0}' was previously defined in this scope", Name); identifier = context.Identifier(Name); } var functionContext = context.MakeFunction(Name); functionContext.Function(FileName, Name); context.PushFrame(); for (var i = 0; i < Arguments.Count; i++) { var name = Arguments[i]; if (!functionContext.DefineArgument(i, name)) throw new MondCompilerException(FileName, Line, "Identifier '{0}' was previously defined in this scope", name); } functionContext.Bind(functionContext.Label); functionContext.Enter(); Block.Compile(functionContext); functionContext.LoadUndefined(); functionContext.Return(); context.PopFrame(); context.Closure(functionContext.Label); if (identifier != null) { if (!isStatement) // statements should leave nothing on the stack context.Dup(); context.Store(identifier); if (isStatement) return 0; } return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = Values.Sum(value => value.Compile(context)); stack += context.NewArray(Values.Count); CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var enumerator = context.DefineInternal("enumerator", true); var stack = 0; var start = context.MakeLabel("foreachStart"); var end = context.MakeLabel("foreachEnd"); // set enumerator stack += Expression.Compile(context); stack += context.LoadField(context.String("getEnumerator")); stack += context.Call(0); stack += context.Store(enumerator); // loop while moveNext returns true stack += context.Bind(start); stack += context.Load(enumerator); stack += context.LoadField(context.String("moveNext")); stack += context.Call(0); stack += context.JumpFalse(end); // loop body context.PushScope(); context.PushLoop(start, end); if (!context.DefineIdentifier(Identifier)) { throw new MondCompilerException(FileName, Line, CompilerError.IdentifierAlreadyDefined, Identifier); } var identifier = context.Identifier(Identifier); stack += context.Load(enumerator); stack += context.LoadField(context.String("current")); stack += context.Store(identifier); stack += Block.Compile(context); stack += context.Jump(start); context.PopLoop(); context.PopScope(); // after loop stack += context.Bind(end); stack += context.Load(enumerator); stack += context.LoadField(context.String("dispose")); stack += context.Call(0); stack += context.Drop(); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; stack += Left.Compile(context); stack += context.LoadField(context.String(Name)); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var isAssignment = false; var needResult = !(Parent is IBlockExpression); switch (Operation) { case TokenType.Increment: stack += context.Load(context.Number(1)); stack += Right.Compile(context); stack += context.BinaryOperation(TokenType.Add); isAssignment = true; break; case TokenType.Decrement: stack += context.Load(context.Number(1)); stack += Right.Compile(context); stack += context.BinaryOperation(TokenType.Subtract); isAssignment = true; break; case TokenType.Subtract: case TokenType.Not: case TokenType.BitNot: stack += Right.Compile(context); stack += context.UnaryOperation(Operation); break; default: throw new NotSupportedException(); } if (isAssignment) { var storable = Right as IStorableExpression; if (storable == null) { throw new MondCompilerException(FileName, Line, CompilerError.LeftSideMustBeStorable); } if (needResult) { stack += context.Dup(); } stack += storable.CompileStore(context); } CheckStack(stack, needResult ? 1 : 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); if (Value) context.LoadTrue(); else context.LoadFalse(); return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var target = context.BreakLabel(); if (target == null) throw new MondCompilerException(FileName, Line, "Unresolved jump"); context.Jump(target); return 0; }
public void CompileTailCall(FunctionContext context) { context.Line(FileName, Line); foreach (var argument in Arguments) { CompileCheck(context, argument, 1); } context.TailCall(Arguments.Count, context.Label); }
public int CompileTailCall(FunctionContext context) { context.Line(FileName, Line); var stack = Arguments.Sum(argument => argument.Compile(context)); stack += context.TailCall(Arguments.Count, context.Label); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var isAssignment = false; var stack = 0; switch (Operation) { case TokenType.Increment: context.Load(context.Number(1)); CompileCheck(context, Right, 1); context.BinaryOperation(TokenType.Add); isAssignment = true; break; case TokenType.Decrement: context.Load(context.Number(1)); CompileCheck(context, Right, 1); context.BinaryOperation(TokenType.Subtract); isAssignment = true; break; case TokenType.Subtract: case TokenType.LogicalNot: CompileCheck(context, Right, 1); context.UnaryOperation(Operation); stack++; break; default: throw new NotSupportedException(); } if (isAssignment) { var storable = Right as IStorableExpression; if (storable == null) throw new MondCompilerException(FileName, Line, "The left-hand side of an assignment must be storable"); // TODO: better message var needResult = !(Parent is BlockExpression); if (needResult) { context.Dup(); stack++; } storable.CompileStore(context); } return stack; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = Arguments.Sum(argument => argument.Compile(context)); stack += Method.Compile(context); stack += context.Call(Arguments.Count); CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); foreach (var value in Values) { CompileCheck(context, value, 1); } context.NewArray(Values.Count); return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var sequenceContext = context as SequenceBodyContext; if (sequenceContext == null) { throw new MondCompilerException(FileName, Line, CompilerError.YieldInFun); } return(context.Jump(sequenceContext.SequenceBody.EndLabel)); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var target = context.ContinueLabel(); if (target == null) { throw new MondCompilerException(FileName, Line, CompilerError.UnresolvedJump); } return(context.Jump(target)); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; stack += Left.Compile(context); stack += Index.Compile(context); stack += context.LoadArray(); CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); foreach (var argument in Arguments) { CompileCheck(context, argument, 1); } CompileCheck(context, Method, 1); context.Call(Arguments.Count); return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); if (Value) { context.LoadTrue(); } else { context.LoadFalse(); } return(1); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); context.NewObject(); foreach (var value in Values) { context.Dup(); CompileCheck(context, value.Value, 1); context.Swap(); context.StoreField(context.String(value.Key)); } return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var falseLabel = context.MakeLabel("ternaryFalse"); var endLabel = context.MakeLabel("ternaryEnd"); CompileCheck(context, Condition, 1); context.JumpFalse(falseLabel); CompileCheck(context, IfTrue, 1); context.Jump(endLabel); context.Bind(falseLabel); CompileCheck(context, IfFalse, 1); context.Bind(endLabel); return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var branchLabels = new List <LabelOperand>(Branches.Count); for (var i = 0; i < Branches.Count; i++) { branchLabels.Add(context.MakeLabel("ifBranch_" + i)); } var branchElse = context.MakeLabel("ifElse"); var branchEnd = context.MakeLabel("ifEnd"); for (var i = 0; i < Branches.Count; i++) { var branch = Branches[i]; stack += branch.Condition.Compile(context); stack += context.JumpTrue(branchLabels[i]); } stack += context.Jump(branchElse); for (var i = 0; i < Branches.Count; i++) { var branch = Branches[i]; stack += context.Bind(branchLabels[i]); stack += branch.Block.Compile(context); stack += context.Jump(branchEnd); } stack += context.Bind(branchElse); if (Else != null) { stack += Else.Block.Compile(context); } stack += context.Bind(branchEnd); CheckStack(stack, 0); return(0); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var start = context.MakeLabel("doWhileStart"); var end = context.MakeLabel("doWhileEnd"); context.Bind(start); context.PushLoop(start, end); CompileCheck(context, Block, 0); context.PopLoop(); CompileCheck(context, Condition, 1); context.JumpTrue(start); context.Bind(end); return 0; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var falseLabel = context.MakeLabel("ternaryFalse"); var endLabel = context.MakeLabel("ternaryEnd"); stack += Condition.Compile(context); stack += context.JumpFalse(falseLabel); CheckStack(IfTrue.Compile(context), 1); stack += context.Jump(endLabel); stack += context.Bind(falseLabel); CheckStack(IfFalse.Compile(context), 1); stack += context.Bind(endLabel); CheckStack(stack, 0); return(1); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var start = context.MakeLabel("forStart"); var increment = context.MakeLabel("forContinue"); var end = context.MakeLabel("forEnd"); context.PushScope(); if (Initializer != null) { stack += Initializer.Compile(context); } context.Bind(start); if (Condition != null) { stack += Condition.Compile(context); stack += context.JumpFalse(end); } context.PushLoop(increment, end); stack += Block.Compile(context); context.PopLoop(); stack += context.Bind(increment); if (Increment != null) { stack += Increment.Compile(context); } stack += context.Jump(start); stack += context.Bind(end); context.PopScope(); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; stack += context.NewObject(); foreach (var value in Values) { stack += context.Dup(); stack += value.Value.Compile(context); stack += context.Swap(); stack += context.StoreField(context.String(value.Key)); } CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var shouldBeGlobal = context.FrameIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal; foreach (var declaration in Declarations) { var name = declaration.Name; if (!shouldBeGlobal) { if (!context.DefineIdentifier(name, IsReadOnly)) { throw new MondCompilerException(FileName, Line, CompilerError.IdentifierAlreadyDefined, name); } } if (declaration.Initializer == null) { continue; } if (!shouldBeGlobal) { var identifier = context.Identifier(name); stack += declaration.Initializer.Compile(context); stack += context.Store(identifier); } else { stack += declaration.Initializer.Compile(context); stack += context.LoadGlobal(); stack += context.StoreField(context.String(name)); } } CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var identifier = context.Identifier(Name); /*if (identifier == null) throw new MondCompilerException(FileName, Line, "Undefined identifier '{0}'", Name);*/ if (identifier == null) { context.LoadGlobal(); context.LoadField(context.String(Name)); } else { context.Load(identifier); } return 1; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var branchLabels = new List<LabelOperand>(Branches.Count); for (var i = 0; i < Branches.Count; i++) { branchLabels.Add(context.MakeLabel("ifBranch_" + i)); } var branchElse = context.MakeLabel("ifElse"); var branchEnd = context.MakeLabel("ifEnd"); for (var i = 0; i < Branches.Count; i++) { var branch = Branches[i]; CompileCheck(context, branch.Condition, 1); context.JumpTrue(branchLabels[i]); } context.Jump(branchElse); for (var i = 0; i < Branches.Count; i++) { var branch = Branches[i]; context.Bind(branchLabels[i]); CompileCheck(context, branch.Block, 0); context.Jump(branchEnd); } context.Bind(branchElse); if (Else != null) CompileCheck(context, Else.Block, 0); context.Bind(branchEnd); return 0; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); if (context is SequenceBodyContext) { throw new MondCompilerException(FileName, Line, CompilerError.ReturnInSeq); } var stack = 0; if (context.AssignedName != null) { var callExpression = Value as CallExpression; if (callExpression != null) { var identifierExpression = callExpression.Method as IdentifierExpression; if (identifierExpression != null && context.Identifier(identifierExpression.Name) == context.AssignedName) { stack += callExpression.CompileTailCall(context); CheckStack(stack, 0); return(stack); } } } if (Value != null) { stack += Value.Compile(context); } else { stack += context.LoadUndefined(); } stack += context.Return(); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); foreach (var declaration in Declarations) { var name = declaration.Name; if (!context.DefineIdentifier(name)) throw new MondCompilerException(FileName, Line, "Identifier '{0}' was previously defined in this scope", name); if (declaration.Initializer != null) { var identifier = context.Identifier(name); CompileCheck(context, declaration.Initializer, 1); context.Store(identifier); } } return 0; }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var start = context.MakeLabel("doWhileStart"); var cont = context.MakeLabel("doWhileContinue"); var end = context.MakeLabel("doWhileEnd"); stack += context.Bind(start); context.PushLoop(cont, end); stack += Block.Compile(context); context.PopLoop(); stack += context.Bind(cont); stack += Condition.Compile(context); stack += context.JumpTrue(start); stack += context.Bind(end); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); return(context.LoadGlobal()); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); return(context.Load(context.Number(Value))); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; var caseLabels = new List <LabelOperand>(Branches.Count); for (var i = 0; i < Branches.Count; i++) { caseLabels.Add(context.MakeLabel("caseBranch_" + i)); } var caseDefault = context.MakeLabel("caseDefault"); var caseEnd = context.MakeLabel("caseEnd"); stack += Expression.Compile(context); List <JumpTable> tables; List <JumpEntry> rest; var flattenedBranches = FlattenBranches(Branches, caseLabels); BuildTables(flattenedBranches, caseDefault, out tables, out rest); foreach (var table in tables) { var start = table.Entries[0].Value; var labels = table.Entries.Select(e => e.Label).ToList(); stack += context.Dup(); stack += context.JumpTable(start, labels); } foreach (var entry in rest) { stack += context.Dup(); stack += entry.Condition.Compile(context); stack += context.BinaryOperation(TokenType.EqualTo); stack += context.JumpTrue(entry.Label); } stack += context.Jump(caseDefault); context.PushLoop(null, caseEnd); for (var i = 0; i < Branches.Count; i++) { var branchStack = stack; var branch = Branches[i]; branchStack += context.Bind(caseLabels[i]); branchStack += context.Drop(); branchStack += branch.Block.Compile(context); branchStack += context.Jump(caseEnd); CheckStack(branchStack, 0); } stack += context.Bind(caseDefault); stack += context.Drop(); if (DefaultBlock != null) { stack += DefaultBlock.Compile(context); } context.PopLoop(); stack += context.Bind(caseEnd); CheckStack(stack, 0); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); var stack = 0; TokenType assignOperation; var hasAssignOperation = _assignMap.TryGetValue(Operation, out assignOperation); var isAssign = Operation == TokenType.Assign || hasAssignOperation; if (isAssign) { var storable = Left as IStorableExpression; if (storable == null) { throw new MondCompilerException(FileName, Line, CompilerError.LeftSideMustBeStorable); } var needResult = !(Parent is IBlockExpression); stack += Right.Compile(context); if (hasAssignOperation) { stack += Left.Compile(context); stack += context.BinaryOperation(assignOperation); } if (needResult) { stack += context.Dup(); } stack += storable.CompileStore(context); CheckStack(stack, needResult ? 1 : 0); return(stack); } if (Operation == TokenType.ConditionalOr) { var endOr = context.MakeLabel("endOr"); stack += Left.Compile(context); stack += context.JumpTruePeek(endOr); stack += context.Drop(); stack += Right.Compile(context); stack += context.Bind(endOr); CheckStack(stack, 1); return(stack); } if (Operation == TokenType.ConditionalAnd) { var endAnd = context.MakeLabel("endAnd"); stack += Left.Compile(context); stack += context.JumpFalsePeek(endAnd); stack += context.Drop(); stack += Right.Compile(context); stack += context.Bind(endAnd); CheckStack(stack, 1); return(stack); } stack += Right.Compile(context); stack += Left.Compile(context); stack += context.BinaryOperation(Operation); CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Line(FileName, Line); TokenType assignOperation; var hasAssignOperation = _assignMap.TryGetValue(Operation, out assignOperation); var isAssign = Operation == TokenType.Assign || hasAssignOperation; if (isAssign) { var storable = Left as IStorableExpression; if (storable == null) throw new MondCompilerException(FileName, Line, "The left-hand side of an assignment must be storable"); var needResult = !(Parent is BlockExpression); var stack = 0; CompileCheck(context, Right, 1); if (hasAssignOperation) { CompileCheck(context, Left, 1); context.BinaryOperation(assignOperation); } if (needResult) { context.Dup(); stack++; } storable.CompileStore(context); return stack; } if (Operation == TokenType.LogicalOr) { var endOr = context.MakeLabel("endOr"); CompileCheck(context, Left, 1); context.JumpTruePeek(endOr); CompileCheck(context, Right, 1); context.Bind(endOr); return 1; } if (Operation == TokenType.LogicalAnd) { var endAnd = context.MakeLabel("endAnd"); CompileCheck(context, Left, 1); context.JumpFalsePeek(endAnd); CompileCheck(context, Right, 1); context.Bind(endAnd); return 1; } CompileCheck(context, Right, 1); CompileCheck(context, Left, 1); context.BinaryOperation(Operation); return 1; }