public override void Compile(EmitCompilerContext context, EmitIl il) { Initializer?.Compile(context, il); var start = il.DefineLabel(); var end = il.DefineLabel(); il.MarkLabel(start); if (Predicate != null) { Predicate.Compile(context, il); } else { il.Emit(EmitOpCodes.Ldc_I4_1); } il.Emit(EmitOpCodes.Brfalse, end); Body.Compile(context, il); Incrementor?.Compile(context, il); il.Emit(EmitOpCodes.Br, start); il.MarkLabel(end); }
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) { 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); }
internal override IEnumerable <Instruction> Compile() { List <Instruction> instructions = new List <Instruction>(); instructions.AddRange(Initializer?.Compile() ?? new Instruction[0]); List <Instruction> bodyInstructions = Body.Compile().ToList(); bodyInstructions.AddRange(Incrementor?.Compile() ?? new Instruction[0]); List <Instruction> check; if (Condition == null) { // No condition? We're in a forever loop check = new List <Instruction>(); } else { check = Condition.Compile().ToList(); check.AddRange( Compiler.CompileImplicitConversion( Condition.GetKnownType(), RedwoodType.GetForCSharpType(typeof(bool)) ) ); check.Add(new ConditionalJumpInstruction(2)); // Next instruction (1) + Body + Increment + Jump instruction (1) check.Add(new JumpInstruction(bodyInstructions.Count + 2)); } // Jump back to the beginning of the loop bodyInstructions.Add(new JumpInstruction(-(bodyInstructions.Count + check.Count))); instructions.AddRange(check); instructions.AddRange(bodyInstructions); return(instructions); }
internal override IEnumerable <Instruction> Compile() { List <Instruction> instructions = new List <Instruction>(); if (Initializer == null) { instructions.Add(new LoadConstantInstruction(null)); } else { instructions.AddRange(Initializer.Compile()); } if (Type.GetIndicatedType() != null) { instructions.AddRange( Compiler.CompileImplicitConversion(Initializer?.GetKnownType(), Type.GetIndicatedType()) ); } instructions.Add(Compiler.CompileVariableAssign(DeclaredVariable)); return(instructions); }
public override int Compile(FunctionContext context) { context.Position(Token); var stack = 0; var start = context.MakeLabel("forStart"); var increment = context.MakeLabel("forContinue"); var brk = context.MakeLabel("forBreak"); var end = context.MakeLabel("forEnd"); var containsFunction = new LoopContainsFunctionVisitor(); Block.Accept(containsFunction); context.PushScope(); if (Initializer != null) { var hasCode = true; if (Initializer.Statements.Count > 0) { if (Initializer.Statements[0] is VarExpression initializerVar && initializerVar.Declarations.All(d => d.Initializer == null)) { hasCode = false; } } if (hasCode) { context.Statement(Initializer); } stack += Initializer.Compile(context); } // loop body context.Bind(start); if (Condition != null) { context.Statement(Condition); stack += Condition.Compile(context); stack += context.JumpFalse(end); } var loopContext = containsFunction.Value ? new LoopContext(context) : context; loopContext.PushLoop(increment, containsFunction.Value ? brk : end); if (containsFunction.Value) { stack += loopContext.Enter(); } stack += Block.Compile(loopContext); stack += context.Bind(increment); // continue if (containsFunction.Value) { stack += loopContext.Leave(); } loopContext.PopLoop(); if (Increment != null) { context.Statement(Increment); stack += Increment.Compile(context); } stack += context.Jump(start); if (containsFunction.Value) { stack += context.Bind(brk); // break (with function) stack += context.Leave(); } stack += context.Bind(end); // break (without function) context.PopScope(); CheckStack(stack, 0); 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); }