public override int GenerateFunction(functionExpressionNode node, string name, StatementNode r, out FunctionDelegate _delegate) { DynamicMethod method = new DynamicMethod(name, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, typeof(JSValue), new Type[] { typeof(JSContext) }, typeof(JSContext), false); ILGenerator tmp = method.GetILGenerator(); base.GenerateFunctionBody(r, tmp); _delegate = (FunctionDelegate) method.CreateDelegate(typeof(FunctionDelegate)); base.Source.FunctionList.Add(node); return (base.Source.FunctionList.Count - 1); }
public override int GenerateFunction(functionExpressionNode node, string name, StatementNode r, out FunctionDelegate _delegate) { int n = this.FunctionList.Count; MethodBuilder method = this.tb.DefineMethod(name + "_" + n, MethodAttributes.Static | MethodAttributes.Public, typeof(JSValue), new Type[] { typeof(JSContext) }); method.DefineParameter(1, ParameterAttributes.None, "context"); base.GenerateFunctionBody(r, method.GetILGenerator()); this.FunctionList.Add(new _FncInfo() { args=node.ParameterList.Names, mb = method}); _delegate = null; return n; }
void Write(StatementNode stmt) { #region contract if(stmt == null) throw new ArgumentNullException("stmt"); #endregion _sql.Write(new[] {'\t'}, 0, _indent); if(stmt is SelectNode) Write(stmt as SelectNode); else throw new ApplicationException("Can handle only 'select' statements but got: "+stmt.GetType().Name); }
internal override void ConstructionComplete() { CommonTree CatchTmp = (((CommonTree) base.Children[1]).ChildCount != 0) ? ((CommonTree) ((CommonTree) base.Children[1]).Children[0]) : null; if (CatchTmp != null) { this.CatchTree = (StatementNode) CatchTmp.Children[1]; this.CatchId = CompileContext.EscapeIdentifier(((CommonTree) CatchTmp.Children[0]).Text); } this.FinallyTree = (((CommonTree) base.Children[2]).ChildCount != 0) ? ((StatementNode) ((CommonTree) base.Children[2]).Children[0]) : null; if (this.FinallyTree == null) { this.DeleteChild(2); } if (this.CatchTree == null) { this.DeleteChild(1); } else { this.ReplaceChildren(1, 1, this.CatchTree); } base.ConstructionComplete(); }
public IEnumerable <ValueNode> Default(StatementNode node) => emptyArray;
public void Add(StatementNode stat) { StList.Add(stat); }
void SimpleStatement(ref StatementNode node) { Expect(49); node = new SimpleStatementNode(t.val); }
internal bool TryFindContextNodes(Position position, out StatementNode currentNode, out StatementNode parentNode) { currentNode = null; parentNode = null; if (this.statementNodeRoot == null) { return(false); } currentNode = this.FindInnermostNode(this.statementNodeRoot, position); if (currentNode.Parent == null) { parentNode = currentNode; currentNode = null; } else { parentNode = currentNode.Parent; // For Rtype, determining the parent depends on indentation... if (this.language == ParserLanguage.Rtype) { // We think we've found the parent node, but we may have to climb // back up the tree if we're less indented than the current statement. while (parentNode != null && position.Column <= parentNode.Indent) { parentNode = parentNode.Parent; } // We think we've found the current node, but if we're at the same // or less indent, it *can't* be a continuation of the current // statement. if (position.Column <= currentNode.Indent) { currentNode = null; } } else if (this.language == ParserLanguage.Xml) { // For XML, we have to see if we're *after* the end of // each statement... we might just be in the parent's content! if (currentNode != null) { if (currentNode.Statement.AllTokens.Last().Range.End <= position) { currentNode = null; } } } } return(true); }
private static string FormatTerminal(StatementNode node) { var terminal = node as StatementGrammarNode; if (terminal == null) { return node.Token.Value; } var formatOptions = OracleConfiguration.Configuration.Formatter.FormatOptions; var value = terminal.Token.Value; if (terminal.Id.IsIdentifier() || GrammarSpecificFunctions.Contains(terminal.Id)) { return FormatTerminalValue(value, formatOptions.Identifier); } if (terminal.Id.IsAlias()) { return FormatTerminalValue(value, formatOptions.Alias); } if (terminal.IsReservedWord) { return FormatTerminalValue(value, formatOptions.ReservedWord); } if (!String.Equals(terminal.Id, Terminals.StringLiteral)) { return FormatTerminalValue(value, formatOptions.Keyword); } return value; }
void ReturnStatement(ref StatementNode node) { Expect(36); node = new ReturnStatementNode(); }
public void Add(StatementNode node) { _sts.Add(node); }
public virtual void Visit(StatementNode statement) { }
protected override bool ParseCilInstructionInternal(Instruction instruction, ProgramState state) { switch (instruction.OpCode.Code) { case Code.Box: (var value, var type) = state.Pop(); var boxedValueIdentifier = state.GetIdentifier(Identifier.IdentKind.Normal); var boxedObjectVariable = new VarExpression(boxedValueIdentifier); var boxedObjectType = new BoxedValueType(Tptr.PtrKind.Pk_pointer, new Tstruct("System.Object"), value, type); var callFlags = new Call.CallFlags(false, false, false); // The value in question is boxed into a generic object. var objectAllocationCall = new Call(boxedValueIdentifier, boxedObjectType, new ConstExpression(ProcedureName.BuiltIn__new), new List <Call.CallArg> { new Call.CallArg( new SizeofExpression( boxedObjectType.StripPointer(), "exact"), boxedObjectType) }, callFlags, state.CurrentLocation); var objectConstructorCall = new Call(state.GetIdentifier(Identifier.IdentKind.Normal), new Tvoid(), new ConstExpression(new ProcedureName(".ctor", new List <string>(), "System.Object", "System.Void", false)), new List <Call.CallArg> { new Call.CallArg(boxedObjectVariable, boxedObjectType) }, callFlags, state.CurrentLocation); var node = new StatementNode(state.CurrentLocation, StatementNode.StatementNodeKind.Call, state.ProcDesc, comment: "System.Void System.Object::.ctor()"); node.Instructions.Add(objectAllocationCall); node.Instructions.Add(objectConstructorCall); RegisterNode(state, node); state.PushExpr(boxedObjectVariable, boxedObjectType); state.PushInstruction(instruction.Next, node); state.AppendToPreviousNode = true; return(true); default: return(false); } }
public BlockNode(StatementNode node) { _sts.Add(node); }
public IfNode(ExprNode expr, StatementNode stat) { Expr = expr; Stat_first = stat; }
public IfNode(ExprNode expr, StatementNode stat_first, StatementNode stat_second) { Expr = expr; Stat_first = stat_first; Stat_second = stat_second; }
public ForNode(StatementNode a, ExprNode expr, StatementNode stat) { Assign = a; Expr = expr; Stlist = stat; }
public WhileNode(ExprNode expr, StatementNode stat) { Expr = expr; Stat = stat; }
public WithStatement(uint start, uint end, Token op1, StatementNode[] withItems, Token[] separators, Token colon, Token typeComment, StatementNode suite) : base(start, end) { Operator1 = op1; WithItems = withItems; Separators = separators; Operator2 = colon; TypeComment = typeComment; Suite = suite; }
public RepeatNode(StatementNode stlist, ExprNode expr) { Expr = expr; StList.Add(stlist); }
private static SourcePosition CreateNodePosition(StatementNode movedNode, StatementGrammarNode nodeToExchange) { return(nodeToExchange == null ? SourcePosition.Empty : SourcePosition.Create(nodeToExchange.SourcePosition.IndexStart, GetLastNonChainingNodePosition(nodeToExchange, movedNode.ParentNode.Id))); }
internal AssemblyBuilder GenerateAB(StatementNode r) { base.GenerateGlobalCode(r); MethodInfo mi_PushContext = typeof(JSContext).GetMethod("PushContext", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(JSContext) }, null); MethodInfo mi_PopContext = typeof(JSContext).GetMethod("PopContext", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, new Type[0], null); ILGenerator gen = this.tb.DefineMethod("__Run", MethodAttributes.Public, typeof(JSValue), Type.EmptyTypes).GetILGenerator(); LocalBuilder l_ctx = gen.DeclareLocal(typeof(JSContext)); LocalBuilder value = gen.DeclareLocal(typeof(JSValue)); Label label60 = gen.DefineLabel(); gen.Emit(OpCodes.Newobj, this.ctor1); gen.Emit(OpCodes.Stloc, l_ctx.LocalIndex); gen.Emit(OpCodes.Ldloc, l_ctx.LocalIndex); gen.EmitCallV( mi_PushContext); gen.BeginExceptionBlock(); gen.Emit(OpCodes.Ldloc, l_ctx.LocalIndex); gen.EmitCallV( this.GlobalMethod); gen.Emit(OpCodes.Stloc, value.LocalIndex); gen.Emit(OpCodes.Leave_S, label60); gen.BeginFinallyBlock(); gen.EmitCallV( mi_PopContext); gen.Emit(OpCodes.Endfinally); gen.EndExceptionBlock(); gen.MarkLabel(label60); gen.Emit(OpCodes.Ldloc, value.LocalIndex); gen.Emit(OpCodes.Ret); MethodBuilder method = this.tb.DefineMethod(".cctor", MethodAttributes.HideBySig | MethodAttributes.Static | MethodAttributes.Private); ConstructorInfo ctor2 = typeof(JSDebugFunctionDef).GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null); FieldInfo param_names = typeof(JSDebugFunctionDef).GetField("param_names"); ConstructorInfo ctor4 = typeof(Func<,>).MakeGenericType(new Type[] { typeof(JSContext), typeof(JSValue) }).GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(object), typeof(IntPtr) }, null); MethodInfo set_del = typeof(JSDebugFunctionDef).GetMethod("set_del", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(Func<,>).MakeGenericType(new Type[] { typeof(JSContext), typeof(JSValue) }) }, null); method.SetReturnType(typeof(void)); gen = method.GetILGenerator(); LocalBuilder def = gen.DeclareLocal(typeof(JSDebugFunctionDef)); LocalBuilder strarr = gen.DeclareLocal(typeof(string[])); LocalBuilder defArray = gen.DeclareLocal(typeof(JSDebugFunctionDef[])); gen.Emit(OpCodes.Nop); gen.Emit(OpCodes.Ldc_I4, this.FunctionList.Count); gen.Emit(OpCodes.Newarr, typeof(JSDebugFunctionDef)); gen.Emit(OpCodes.Stloc, defArray.LocalIndex); for (int i = 0; i < this.FunctionList.Count; i++) { gen.Emit(OpCodes.Ldloc, defArray.LocalIndex); gen.Emit(OpCodes.Ldc_I4, i); gen.Emit(OpCodes.Newobj, ctor2); gen.Emit(OpCodes.Stloc, def.LocalIndex); gen.Emit(OpCodes.Ldloc, def.LocalIndex); string[] names = this.FunctionList[i].args; gen.Emit(OpCodes.Ldc_I4, names.Length); gen.Emit(OpCodes.Newarr, typeof(string)); gen.Emit(OpCodes.Stloc, strarr.LocalIndex); for (int j = 0; j < names.Length; j++) { gen.Emit(OpCodes.Ldloc, strarr.LocalIndex); gen.Emit(OpCodes.Ldc_I4, j); gen.Emit(OpCodes.Ldstr, names[j]); gen.Emit(OpCodes.Stelem_Ref); } gen.Emit(OpCodes.Ldloc, strarr.LocalIndex); gen.Emit(OpCodes.Stfld, param_names); gen.Emit(OpCodes.Ldloc, def.LocalIndex); gen.Emit(OpCodes.Ldnull); gen.Emit(OpCodes.Ldftn, this.FunctionList[i].mb); gen.Emit(OpCodes.Newobj, ctor4); gen.EmitCallV( set_del); gen.Emit(OpCodes.Nop); gen.Emit(OpCodes.Ldloc, def.LocalIndex); gen.Emit(OpCodes.Stelem_Ref); } gen.Emit(OpCodes.Ldloc, defArray.LocalIndex); gen.Emit(OpCodes.Stsfld, this.fncListField); gen.Emit(OpCodes.Ret); method = this.tb.DefineMethod("GetFunctionReference", MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Public); ConstructorInfo ctor3 = typeof(JSDebugFunction).GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(JSDebugFunctionDef), typeof(JSEnvRec) }, null); method.SetReturnType(typeof(JSValue)); method.SetParameters(new Type[] { typeof(int), typeof(JSEnvRec) }); method.DefineParameter(1, ParameterAttributes.None, "key"); method.DefineParameter(2, ParameterAttributes.None, "r"); gen = method.GetILGenerator(); LocalBuilder value2 = gen.DeclareLocal(typeof(JSValue)); Label label17 = gen.DefineLabel(); gen.Emit(OpCodes.Nop); gen.Emit(OpCodes.Ldsfld, this.fncListField); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldelem_Ref); gen.Emit(OpCodes.Ldarg_2); gen.Emit(OpCodes.Newobj, ctor3); gen.Emit(OpCodes.Stloc_0); gen.Emit(OpCodes.Br_S, label17); gen.MarkLabel(label17); gen.Emit(OpCodes.Ldloc_0); gen.Emit(OpCodes.Ret); Type t = this.tb.CreateType(); return this.ab; }
private static void GenerateExternalAssembly(StatementNode r) { ASMCompileContext ctx = new ASMCompileContext(); ctx.GenerateAB(r).Save("DynamicAssemblyExample.dll"); }
void Statement(ref StatementNode node) { var stoken = t; switch (la.kind) { case 11: case 12: { EndStatement(ref node); break; } case 46: { OptionStatement(ref node); break; } case 36: { ReturnStatement(ref node); break; } case 37: { IfThenStatement(ref node); break; } case 39: { ForStatement(ref node); break; } case 41: { NextStatement(ref node); break; } case 44: { RestoreStatement(ref node); break; } case 1: case 9: case 29: { LetStatement(ref node); break; } case 42: { DataStatement(ref node); break; } case 13: { PrintStatement(ref node); break; } case 43: { ReadStatement(ref node); break; } case 31: case 32: case 33: { GotoOrSubStatement(ref node); break; } case 45: { DimStatement(ref node); break; } case 48: { OnGotoStatement(ref node); break; } case 49: { SimpleStatement(ref node); break; } case 50: { DefStatement(ref node); break; } case 51: { InputStatement(ref node); break; } default: SynErr(55); break; } if (node != null) { node.Line = stoken.line; node.Col = stoken.col; } }
private static CompiledScript GenerateMethod(StatementNode r, ScriptSource Source) { DynamicMethod method = new DynamicMethod("GlobalCode", MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, typeof(JSValue), new Type[] { typeof(JSContext) }, typeof(JSContext), false); DMCompileContext ctx = new DMCompileContext(method.GetILGenerator()); ctx.Source = Source; ctx.GenerateGlobalCode(r); GlobalCodeDelegate _delegate = (GlobalCodeDelegate) method.CreateDelegate(typeof(GlobalCodeDelegate)); CompiledScript cs = new CompiledScript(_delegate); cs.Source = Source; return cs; }
void RestoreStatement(ref StatementNode node) { Expect(44); node = new RestoreStatementNode(); }
public IfElseNode(ExpressionNode condition, StatementNode body, StatementNode elseBody) { Condition = condition; Body = body; ElseBody = elseBody; }
protected override void Visit(StatementNode node) { Add(node); }
public TryCatchNodeTests() { @try = new BlockNode(SourcePosition.NIL); @catch = new BlockNode(SourcePosition.NIL); subject = new TryCatchNode(SourcePosition.NIL, @try, @catch, new IdentNode(SourcePosition.NIL, "foo")); }
public WhileNode(AssignNode assign, ExprNode expr, StatementNode stat) { Assign = assign; Expr = expr; Stat = stat; }
protected override bool ParseCilInstructionInternal(Instruction instruction, ProgramState state) { switch (instruction.OpCode.Code) { case Code.Ret: Store retInstr; var retType = state.Method.ReturnType.GetElementType(); if (retType == state.Method.Module.TypeSystem.Void) { state.PreviousNode.Successors.Add(state.ProcDesc.ExitNode); } else { var retNode = new StatementNode(state.CurrentLocation, StatementNode.StatementNodeKind.ReturnStmt, state.ProcDesc); (var returnValue, _) = state.Pop(); Expression returnVariable = new LvarExpression( new LocalVariable(Identifier.ReturnIdentifier, state.Method)); if (returnValue is BinopExpression) { // We see that for the auto-generated method op_Inequality in records, // an equality expression is pushed directly onto the stack and // returned. However, return of an expression is not valid in the SIL -- // we must inline a variable store and load of the value prior to // subsequently returning it. var inlineReturn = new LocalVariable("inlineReturn", state.Method); var inlineIdentifier = state.GetIdentifier(Identifier.IdentKind.Normal); var storeInlineReturn = new Store(new LvarExpression(inlineReturn), returnValue, Typ.FromTypeReference(retType), state.CurrentLocation); AddMethodBodyInstructionsToCfg(state, storeInlineReturn); var loadInlineReturn = new Load(inlineIdentifier, new LvarExpression(inlineReturn), Typ.FromTypeReference(retType), state.CurrentLocation); AddMethodBodyInstructionsToCfg(state, loadInlineReturn); retInstr = new Store(returnVariable, new VarExpression(inlineIdentifier), Typ.FromTypeReference(retType), state.CurrentLocation); } else { retInstr = new Store(returnVariable, returnValue, Typ.FromTypeReference(retType), state.CurrentLocation); } retNode.Instructions.Add(retInstr); retNode.Successors = new List <CfgNode> { state.ProcDesc.ExitNode }; RegisterNode(state, retNode); } return(true); default: return(false); } }
public BlockNode(StatementNode stat) { Add(stat); }
internal void GenerateGlobalCode(StatementNode r) { this.gen.Emit(OpCodes.Ldarg_0); this.gen.EmitCallV( mi_JSUndefined_Instance); this.gen.EmitCallV( mi_Set_Returnval); this.GenerateCode(r); this.gen.Emit(OpCodes.Ldarg_0); this.gen.EmitCallV( mi_Get_Returnval); this.gen.Emit(OpCodes.Ret); }
public IfNode(ExprNode expr, StatementNode ifTrue, StatementNode ifFalse = null) { this.expr = expr; this.ifTrue = ifTrue; this.ifFalse = ifFalse; }
private void GenerateCode(StatementNode r) { r.GenCode(this); }
public void Visit(StatementNode node) { }
public void Visit(StatementNode node) { Nodes.Push(new StatementNode(Nodes.Pop())); }
public abstract int GenerateFunction(functionExpressionNode node, string name, StatementNode r, out FunctionDelegate _delegate);
public void Visit(StatementNode node) { node.Node.Accept(this); node.Accept(_visitor); }
protected void GenerateFunctionBody(StatementNode r, ILGenerator gen) { LocalFunctionData oldData = this.fncData; try { this.fncData = new LocalFunctionData(gen, gen.DefineLabel(), gen.DeclareLocal(typeof(JSValue))); r.GenCode(this); gen.EmitCallV(mi_JSUndefined_Instance); gen.Emit(OpCodes.Stloc, this.fncData.return_local); gen.MarkLabel(this.fncData.return_label); gen.Emit(OpCodes.Ldloc, this.fncData.return_local); gen.Emit(OpCodes.Ret); } finally { this.fncData = oldData; } }
public ForNode(AssignNode assign, ExprNode bord, StatementNode body) : this(assign, bord, new IntNumNode(1), body) { }
public override IEnumerable<ParseNode> Parse(ParseNode node) { var c = node.ReadChar(); while (c != '\0') { if (c == '[') { while (c != ']') c = node.ReadChar(); var statement = new StatementNode { Name = "customAttribute", Data = new StringReader(node.Buffer.ToString()) }; yield return statement; c = node.ReadChar(); while (c == ' ' || c == '\r' || c == '\n') { node.ResetBuffer(); c = node.ReadChar(); } } while (c != ' ' && c != '.' && c != '\0') c = node.ReadChar(); var value = node.Buffer.ToString(0, node.Buffer.Length - 1); if (c == ' ') { if (value == "using") { node.ResetBuffer(); var statement = new StatementNode { Name = "using", Data = new StringReader(node.GetStringData()) }; yield return statement; continue; } if (value == "class") { node.ResetBuffer(); c = node.ReadChar(); while (c == ' ') { node.ResetBuffer(); c = node.ReadChar(); } while (c != ' ' && c != '\r' && c != '\n' && c != '\0') c = node.ReadChar(); var statement = new StatementNode { Name = "class", Data = new StringReader(node.Buffer.ToString()) }; yield return statement; continue; } if (value == "public") { node.ResetBuffer(); var statement = new StatementNode { Name = "accessModifier", Data = new StringReader("public") }; yield return statement; c = node.ReadChar(); while (c == ' ') c = node.ReadChar(); continue; } if (value == "namespace") { node.ResetBuffer(); var statement = new StatementNode() { Name = "namespace", Data = new StringReader(node.GetStringData()) }; yield return statement; continue; } } yield break; } yield break; }
public BlockNode(StatementNode stat) { StList = new List <StatementNode>(); StList.Add(stat); }
//There are a couple of ways to accomplish this... //When I hit {, I could go into recursive mode //Alternatively, when I hit { I could seek ahead for the end node //This causes hard to follow looping logic if done correctly though public override IEnumerable<ParseNode> Parse(ParseNode node) { var c = node.ReadChar(); while (c != '\0') { while (c != ';' && c != '{' && c != '\0' && c != '/' && c != '\'' && c != '"') c = node.ReadChar(); if (c == '"') { c = ProcessStringLiteral(node); continue; } if (c == '/') { c = node.ReadChar(); if (c == '/') { while (c != '\r') c = node.ReadChar(); } } if (c == '\'') { c = ProcessCharacterLiteral(node); continue; } if (c == ';') { var statement = new StatementNode { Name = "statement", Data = new StringReader(node.Buffer.ToString().TrimStart(' ', '\r', '\n', '\t')) }; node.ResetBuffer(); c = node.ReadChar(); statement.Children = ApplyTemplates(statement).ToArray(); yield return statement; continue; } if (c == '{') { var nestCount = 1; while (nestCount > 0) { c = node.ReadChar(); //Would be much better to create CharacterLiteral, StringLiteral, Comment classes, then parse them if (c == '/') { c = node.ReadChar(); if (c == '/') { while (c != '\r') c = node.ReadChar(); } } if (c == '\'') { ProcessCharacterLiteral(node); continue; } if (c == '{') nestCount++; if (c == '}') nestCount--; } var stringData = node.Buffer.ToString().TrimStart(' ', '\r', '\n', '\t'); var firstBracket = stringData.IndexOf('{'); var innerData = stringData.Substring(firstBracket + 1, stringData.Length - firstBracket - 2).Trim(); var children = new List<ParseNode>(); var statementNode = new StatementNode { Name = "command", Data = new StringReader(stringData.Substring(0, firstBracket - 1).Trim()) }; statementNode.Children = ApplyTemplates(statementNode).ToArray(); children.Add(statementNode); var nestedNode = new NestedStatementNode { CommandData = stringData.Substring(0, firstBracket - 1).Trim(), Name = "nestedStatement", Data = new StringReader(innerData) }; children.AddRange(ApplyTemplates(nestedNode)); nestedNode.Children = children; yield return nestedNode; node.ResetBuffer(); c = node.ReadChar(); continue; } yield break; } }
public ForNode(StatementNode initialization, StatementNode condition, StatementNode final, StatementNode block) { Initialization = initialization; Condition = condition; Final = final; Block = block; }