private static void _replaceDebug(CompilerTarget t, IList<AstNode> block, bool debugging) { for (var i = 0; i < block.Count; i++) { var stmt = block[i] as AstGetSet; //look for calls if (_isDebugCall(stmt)) { System.Diagnostics.Debug.Assert(stmt != null); //Found a call to debug block.RemoveAt(i); if (debugging) { for (var j = 0; j < stmt.Arguments.Count; j++) { var arg = stmt.Arguments[j] as AstIndirectCall; AstReference refNode; if (arg != null && (refNode = arg.Subject as AstReference) != null) { var printlnCall = t.Factory.Call(stmt.Position, EntityRef.Command.Create(Engine.PrintLineAlias)); var concatCall = t.Factory.Call(stmt.Position, EntityRef.Command.Create(Engine.ConcatenateAlias)); var consts = new AstConstant( stmt.File, stmt.Line, stmt.Column, String.Concat("DEBUG ", refNode.Entity, " = ")); concatCall.Arguments.Add(consts); concatCall.Arguments.Add(arg); printlnCall.Arguments.Add(concatCall); block.Insert(i, printlnCall); i += 1; } //end if arg not null } //end for arguments } //end if debugging continue; } //end if debug call var cond = block[i] as AstCondition; //look for conditions if (cond != null) { var expr = cond.Condition as AstIndirectCall; AstReference refNode; EntityRef.Command cmd; if (expr != null && (refNode = expr.Subject as AstReference) != null && refNode.Entity.TryGetCommand(out cmd) && Engine.StringsAreEqual(cmd.Id,Engine.DebugAlias) ) cond.Condition = new AstConstant(expr.File, expr.Line, expr.Column, debugging); } //Recursively replace 'debug' in nested blocks. var complex = block[i] as IAstHasBlocks; if (complex != null) foreach (var subBlock in complex.Blocks) _replaceDebug(t, subBlock, debugging); } //end for statements }
/// <summary> /// Implementation of the debug hook. /// </summary> /// <param name = "t">The compiler target that is to be transformed.</param> public static void Hook(CompilerTarget t) { var debugging = IsDebuggingEnabled(t.Function); _replaceDebug(t, t.Ast, debugging); }
/// <summary> /// Executes the compiler hook (either calls the managed /// delegate or indirectly calls the <see cref = "PValue" /> in the context of the <see cref = "Loader" />.) /// </summary> /// <param name = "target">The compiler target to modify.</param> public void Execute(CompilerTarget target) { try { target.Loader.Options.TargetApplication._SuppressInitialization = true; if (IsManaged) _managed(target); else _interpreted.IndirectCall( target.Loader, new[] {target.Loader.CreateNativePValue(target)}); } finally { target.Loader.Options.TargetApplication._SuppressInitialization = false; } }
/// <summary> /// Applies the encapsulated custom resolver to the supplied AST node. /// </summary> /// <param name = "t">The compiler target for which to resolve the node.</param> /// <param name = "unresolved">The unresolved AST node.</param> /// <returns>Null if no solution has been found. A compatible AST node otherwise.</returns> public AstExpr Resolve(CompilerTarget t, AstUnresolved unresolved) { if (IsManaged) { return _managed(t, unresolved); } else if (IsInterpreted) { var presult = _interpreted.IndirectCall ( t.Loader, new[] { t.Loader.CreateNativePValue(t), t.Loader.CreateNativePValue(unresolved) }); if (presult.Type is ObjectPType) return (AstExpr) presult.Value; else return null; } else { throw new InvalidOperationException( "Invalid custom resolver. No implementation provided."); } }
public CompilerTargetAstFactory(CompilerTarget target) { _target = target; }
public CompilerTarget(Loader loader, PFunction function, CompilerTarget parentTarget = null, ISourcePosition position = null) { if (loader == null) throw new ArgumentNullException("loader"); if (function == null) function = loader.ParentApplication.CreateFunction(); if (!ReferenceEquals(function.ParentApplication, loader.ParentApplication)) throw new ArgumentException( Resources.CompilerTarget_Cannot_create_for_foreign_function, "function"); _loader = loader; _function = function; _parentTarget = parentTarget; _importScope = SymbolStore.Create(parentTarget == null ? loader.Symbols : parentTarget.Symbols); _ast = AstBlock.CreateRootBlock(position ?? new SourcePosition("",-1,-1), SymbolStore.Create(ImportScope), AstBlock.RootBlockName, Guid.NewGuid().ToString("N")); }