public override int Compile(FunctionContext context) { context.Position(Token); var stack = Initializer?.Compile(context) ?? 1; var global = context.ArgIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal; foreach (var field in Fields) { var name = field.Alias ?? field.Name; stack += context.Dup(); stack += context.LoadField(context.String(field.Name)); if (global) { stack += context.LoadGlobal(); stack += context.StoreField(context.String(name)); } else { if (!context.DefineIdentifier(name, IsReadOnly)) { throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, name); } stack += context.Store(context.Identifier(name)); } } stack += context.Drop(); CheckStack(stack, 0); return(-1); }
public override int Compile(FunctionContext context) { var stack = 0; var isAssignment = false; var needResult = !(Parent is IBlockExpression); switch (Operation) { case TokenType.Increment: stack += Right.Compile(context); stack += context.Load(context.Number(1)); context.Position(Token); // debug info stack += context.BinaryOperation(TokenType.Add); isAssignment = true; break; case TokenType.Decrement: stack += Right.Compile(context); stack += context.Load(context.Number(1)); context.Position(Token); // debug info stack += context.BinaryOperation(TokenType.Subtract); isAssignment = true; break; case TokenType.Subtract: case TokenType.Not: case TokenType.BitNot: stack += Right.Compile(context); context.Position(Token); // debug info stack += context.UnaryOperation(Operation); break; default: throw new NotSupportedException(); } if (isAssignment) { var storable = Right as IStorableExpression; if (storable == null) { throw new MondCompilerException(this, CompilerError.LeftSideMustBeStorable); } if (needResult) { stack += context.Dup(); } stack += storable.CompilePreLoadStore(context, 1); stack += storable.CompileStore(context); } CheckStack(stack, needResult ? 1 : 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 int CompilePreLoadStore(FunctionContext context, int times) { var stack = 0; stack += Left.Compile(context); for (var i = 1; i < times; i++) { stack += context.Dup(); } return(stack); }
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 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) { var stack = 0; context.Position(Token); stack += context.NewArray(Values.Count); for (var i = 0; i < Values.Count; ++i) { stack += context.Dup(); stack += Values[i].Compile(context); stack += context.Swap(); stack += context.Load(context.Number(i)); stack += context.StoreArray(); } CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Position(Token); var stack = 0; var caseLabels = new List <LabelOperand>(Branches.Count); var caseEnd = context.MakeLabel("caseEnd"); LabelOperand caseDefault = null; BlockExpression defaultBlock = null; for (var i = 0; i < Branches.Count; i++) { var label = context.MakeLabel("caseBranch_" + i); caseLabels.Add(label); var conditions = Branches[i].Conditions; if (conditions.Any(c => c == null)) { caseDefault = label; if (conditions.Count == 1) { defaultBlock = Branches[i].Block; } } } var emptyDefault = caseDefault == null; if (emptyDefault) { caseDefault = context.MakeLabel("caseDefault"); } context.PushScope(); context.Statement(Expression); stack += Expression.Compile(context); var flattenedBranches = FlattenBranches(Branches, caseLabels, caseDefault); BuildTables(flattenedBranches, caseDefault, out var tables, out var 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); } // only bind if we need a default block if (emptyDefault || defaultBlock != null) { stack += context.Bind(caseDefault); } // always drop the switch value stack += context.Drop(); if (defaultBlock != null) { stack += defaultBlock.Compile(context); } context.PopLoop(); stack += context.Bind(caseEnd); context.PopScope(); CheckStack(stack, 0); 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; }
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); 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 void CompileBody(FunctionContext context) { var state = context.DefineInternal("state"); var enumerable = context.DefineInternal("enumerable"); var stack = 0; var bodyToken = new Token(Token, TokenType.Fun, null); var body = new SequenceBodyExpression(bodyToken, null, Block, "moveNext", state, enumerable); var seqContext = new SequenceContext(context.Compiler, "moveNext", body, context); var getEnumerator = context.MakeFunction("getEnumerator"); getEnumerator.Function(getEnumerator.FullName); getEnumerator.Bind(getEnumerator.Label); getEnumerator.Enter(); getEnumerator.Load(enumerable); getEnumerator.Return(); var dispose = context.MakeFunction("dispose"); dispose.Function(dispose.FullName); dispose.Bind(dispose.Label); dispose.Enter(); dispose.LoadUndefined(); dispose.Return(); stack += context.Bind(context.Label); stack += context.Enter(); if (OtherArguments != null) { stack += context.VarArgs(Arguments.Count); } stack += context.Load(context.Number(0)); stack += context.Store(state); stack += context.NewObject(); stack += context.Dup(); stack += context.LoadUndefined(); stack += context.Swap(); stack += context.StoreField(context.String("current")); stack += context.Dup(); stack += body.Compile(seqContext); stack += context.Swap(); stack += context.StoreField(context.String("moveNext")); stack += context.Dup(); stack += context.Closure(getEnumerator.Label); stack += context.Swap(); stack += context.StoreField(context.String("getEnumerator")); stack += context.Dup(); stack += context.Closure(dispose.Label); stack += context.Swap(); stack += context.StoreField(context.String("dispose")); stack += context.Store(enumerable); stack += context.Load(enumerable); stack += context.Return(); CheckStack(stack, 0); }
public override int Compile(FunctionContext context) { var isStatement = Parent is IBlockExpression; var shouldBeGlobal = context.ArgIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal; var shouldStore = Name != null; IdentifierOperand identifier = null; if (shouldStore && !shouldBeGlobal) { if (!context.DefineIdentifier(Name, true)) { throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, Name); } identifier = context.Identifier(Name); } // compile body var functionContext = context.MakeFunction(Name ?? DebugName); functionContext.Function(functionContext.FullName); functionContext.Position(Token); functionContext.PushScope(); for (var i = 0; i < Arguments.Count; i++) { var name = Arguments[i]; if (!functionContext.DefineArgument(i, name)) { throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, name); } } if (OtherArguments != null && !functionContext.DefineArgument(Arguments.Count, OtherArguments)) { throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, OtherArguments); } CompileBody(functionContext); functionContext.PopScope(); // assign result var stack = 0; context.Position(Token); // debug info stack += context.Closure(functionContext.Label); if (shouldStore) { if (!isStatement) // statements should leave nothing on the stack { stack += context.Dup(); } if (!shouldBeGlobal) { stack += context.Store(identifier); } else { stack += context.LoadGlobal(); stack += context.StoreField(context.String(Name)); } if (isStatement) { CheckStack(stack, 0); return(stack); } } CheckStack(stack, 1); return(stack); }
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) { var stack = 0; var isAssignOperation = _assignMap.TryGetValue(Operation, out var assignOperation); if (IsAssign) { var storable = Left as IStorableExpression; if (storable == null) { throw new MondCompilerException(this, CompilerError.LeftSideMustBeStorable); } var needResult = !(Parent is IBlockExpression); if (isAssignOperation) { int preTotal; var preTimes = needResult ? 3 : 2; stack += preTotal = storable.CompilePreLoadStore(context, preTimes); stack += storable.CompileLoad(context); stack += Right.Compile(context); stack += context.BinaryOperation(assignOperation); switch (preTotal / preTimes) { case 0: break; case 1: stack += context.Swap(); break; case 2: stack += context.Swap1For2(); break; default: throw new NotSupportedException(); } stack += storable.CompileStore(context); if (needResult) { stack += storable.CompileLoad(context); } } else { stack += Right.Compile(context); if (needResult) { stack += context.Dup(); } stack += storable.CompilePreLoadStore(context, 1); 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 += Left.Compile(context); stack += Right.Compile(context); context.Position(Token); // debug info stack += context.BinaryOperation(Operation); CheckStack(stack, 1); return(stack); }
public override int Compile(FunctionContext context) { context.Position(Token); var stack = Initializer?.Compile(context) ?? 1; var global = context.ArgIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal; var hasSlice = false; var headSize = 0; var tailSize = 0; foreach (var index in Indices) { if (index.IsSlice) { if (hasSlice) { throw new InvalidOperationException($"Multiple slices in {nameof(DestructuredArrayExpression)}"); } hasSlice = true; } else if (hasSlice) { tailSize++; } else { headSize++; } } var fixedSize = headSize + tailSize; stack += context.Dup(); stack += context.LoadField(context.String("length")); stack += context.Call(0, new List <ImmediateOperand>()); var inTail = false; var fixedI = 0; for (var i = 0; i < Indices.Count; i++) { var index = Indices[i]; var assign = context.MakeLabel("arrayDestructureAssign"); var destruct = context.MakeLabel("arrayDestructureIndex"); if (index.IsSlice) { inTail = true; } if (i < Indices.Count - 1) { stack += context.Dup2(); // -> init.length(), init } stack += context.Load(context.Number(index.IsSlice ? fixedSize : fixedI)); stack += context.BinaryOperation(TokenType.GreaterThan); stack += context.JumpTrue(destruct); stack += context.Drop(); // drops initializer stack += index.IsSlice ? context.NewArray(0) : context.LoadUndefined(); stack += context.Jump(assign); stack += context.Bind(destruct); if (index.IsSlice) { stack += context.Load(context.Number(fixedI)); stack += context.Load(context.Number(-tailSize - 1)); stack += context.LoadUndefined(); stack += context.Slice(); } else { stack += context.Load(context.Number(inTail ? fixedI - fixedSize : fixedI)); stack += context.LoadArray(); } stack += context.Bind(assign); if (global) { stack += context.LoadGlobal(); stack += context.StoreField(context.String(index.Name)); } else { if (!context.DefineIdentifier(index.Name, IsReadOnly)) { throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, index.Name); } stack += context.Store(context.Identifier(index.Name)); } if (!index.IsSlice) { fixedI++; } } CheckStack(stack, 0); return(-1); }