public List <SynFuncDecl> GetFunctionList(string functionName, SynScope scope) { List <SynFuncDecl> ret = new List <SynFuncDecl>(); this.FillFunctionList(functionName, scope, ret); return(ret); }
protected void FillFunctionList(string functionName, SynScope scope, List <SynFuncDecl> lst) { foreach (var v in this.functions) { if (v.Key != functionName) { continue; } foreach (SynFuncDecl sfn in v.Value) { if (sfn.functionName != functionName) { continue; } if (scope == this || sfn.isStatic) { lst.Add(sfn); } } } if (this.parentScope != null) { this.parentScope.FillFunctionList(functionName, scope, lst); } }
public static SynStruct Parse(SynScope parentScope, List <Token> tokens) { if (tokens[0].Matches(TokenType.tyWord, "struct") == false) { return(null); } // TODO: Check that the name is available if (tokens[1].Matches(TokenType.tyWord) == false) { throw new SynthExceptionSyntax(tokens[1], "Missing struct name."); } if (tokens[2].Matches(TokenType.tySymbol, "{") == false) { throw new SynthExceptionSyntax(tokens[2], "Missing struct body entry."); } int idx = 2; Parser.MovePastScopeTSemi(ref idx, tokens); SynStruct ret = new SynStruct(parentScope, tokens[0].fragment); ret.typeName = tokens[1].fragment; ret.declarationTokens = tokens.GetRange(0, idx); tokens.RemoveRange(0, idx); ret.BreakApartParsedTokens(); return(ret); }
public static SynRegion Parse(SynScope parentScope, List <Token> tokens) { int idx = 0; if (tokens[idx].Matches(TokenType.tyWord, "region") == false) { return(null); } ++idx; if (tokens[idx].Matches(TokenType.tyWord) == false) { throw new SynthExceptionSyntax(tokens[idx], $"Invalid region name."); } string scopeName = tokens[idx].fragment; ++idx; if (tokens[idx].Matches(TokenType.tySymbol, "{") == false) { throw new SynthExceptionSyntax(tokens[idx], $"Expected region to start with an opening brace."); } int bodyStart = idx; int end = bodyStart; Parser.MovePastCurlScope(ref end, tokens); SynRegion ret = new SynRegion(parentScope); ret.name = scopeName; ret.bodyStart = bodyStart; ret.declPhrase = tokens.GetRange(0, end); tokens.RemoveRange(0, end); ret.BreakApartParsedTokens(); return(ret); }
public SynType(SynScope parent, string name, bool intrinsic) : base(parent) { this.typeName = name; this.intrinsic = intrinsic; }
// TODO: Perhaps this function should be moved into WASMBuild? public SynNestingBuilder BuildFunction(SynFuncDecl fnd) { SynNestingBuilder builder = new SynNestingBuilder(null); if (fnd.ast != null) { throw new SynthExceptionImpossible($"Attempting to build function {fnd.functionName} AST multiple times."); } SynLog.Log($"Entered SynthFuncDecl.Build() for {fnd.functionName}."); // NOTE: For now functions are non-typed (in the SynthSyn language) and // are non addressable. fnd.ast = new AST( fnd.declPhrase[0], builder, ASTOp.FunctionDecl, fnd, null, false, AST.DataManifest.NoData); SynLog.Log($"Building function AST for {fnd.functionName}."); SynLog.Log(""); List <SynNestingBuilder> builders = new List <SynNestingBuilder>(); builders.Add(builder); //List<TokenTree> treeLines = new List<TokenTree>(); for (int i = 0; i < fnd.executingLines.Count; ++i) { List <Token> execLine = fnd.executingLines[i]; SynLog.LogFragments(execLine); TokenTree rootLineNode = TokenTree.EatTokensIntoTree(execLine, fnd, true); SynLog.LogTree(rootLineNode); // If it's a member function (not a static function) then full in the struct // we belong to as a he invoking scope. Or else set it to null. Its syntax scope // it still all the way where the source code is, but doesn't have a "this" member // function. SynScope invokingScope = null; if (fnd.isStatic == false) { invokingScope = fnd.GetStructScope(); } AST exprAST = builder.ProcessFunctionExpression(builders, this, invokingScope, fnd, rootLineNode); if (exprAST == null) { // We shouldn't have received null, we should have thrown before this throw new SynthExceptionImpossible(""); //TODO: } fnd.ast.branches.Add(exprAST); } fnd.ast.branches.Add(new AST(new Token(), builder, ASTOp.EndScope, builder, null, false, AST.DataManifest.NoData)); SynLog.Log(""); SynLog.Log($"Encountered {builders.Count} nested scopes."); for (int i = 0; i < builders.Count; ++i) { SynLog.LogIndent(0, $"Scope {i}"); SynLog.LogIndent(1, $"Line number : {builders[i].lineNumber}"); SynLog.LogIndent(1, $"Stack Elements : Ele {builders[i].locStkEle.Count} - Vars {builders[i].locStkVars.Count}"); SynLog.LogIndent(1, $"Memory Stack : Vars {builders[i].memStkVars.Count}"); SynLog.LogIndent(1, $"Total Memory Stack : Bytes {builders[i].totalMemoryStackBytes}"); } SynLog.Log(""); SynLog.Log(fnd.ast.DumpDiagnostic()); SynLog.Log($"Finished building AST : {fnd.functionName}."); SynLog.Log($"Converting AST to binary WASM : {fnd.functionName}."); // Before the AST is turned into actual WASM binary, we need to finalize the // byte alignment and the indices of local stack variables. This is done with // CompileAlignment, who's lower scopes should appear earlier in the builders // list before higher children scopes. for (int i = 0; i < builders.Count; ++i) { if (i == 0) { builders[i].CompileAlignment(fnd); } else { builders[i].CompileAlignment(); } } // Gather all the local variables and declare them. This isn't as efficient as things // could be because after variables go out of scope, their positions on the stack // can be reused if they match types we encounter in the future, but that can be // handled later. // The program binary WASMByteBuilder fnBuild = new WASMByteBuilder(); // Gather all the local variables List <WASM.Bin.TypeID> localVarTys = new List <WASM.Bin.TypeID>(); foreach (SynNestingBuilder b in builders) { foreach (ValueRef stkVal in b.locStkEle) { if (stkVal.varType.intrinsic == false) { throw new SynthExceptionCompile(""); // TODO: Error msg } switch (stkVal.varType.typeName) { case "bool": case "int8": case "uint8": case "int16": case "uint16": case "int": case "int32": localVarTys.Add(WASM.Bin.TypeID.Int32); break; case "int64": case "uint64": localVarTys.Add(WASM.Bin.TypeID.Int64); break; case "float": localVarTys.Add(WASM.Bin.TypeID.Float32); break; case "double": localVarTys.Add(WASM.Bin.TypeID.Float64); break; } } } List <KeyValuePair <WASM.Bin.TypeID, int> > consolidatedLocalTys = new List <KeyValuePair <WASM.Bin.TypeID, int> >(); for (int i = 0; i < localVarTys.Count; ++i) { WASM.Bin.TypeID tyid = localVarTys[i]; int ct = 1; ++i; for (; i < localVarTys.Count; ++i) { if (localVarTys[i] != tyid) { break; } ++ct; } consolidatedLocalTys.Add(new KeyValuePair <WASM.Bin.TypeID, int>(tyid, ct)); } fnBuild.AddLEB128((uint)consolidatedLocalTys.Count); // Local decl for (int i = 0; i < consolidatedLocalTys.Count; ++i) { fnBuild.AddLEB128((uint)consolidatedLocalTys[i].Value); // These are really bytes, but they'll end up being added as bytes since they're // small constants fnBuild.AddLEB128((uint)consolidatedLocalTys[i].Key); } builder.BuildBSFunction(fnd, fnd.ast, this, builder, fnBuild); fnBuild.Add_End(); fnd.fnBin = fnBuild.Bin(); SynLog.Log($"Exiting SynthFuncDecl.Build({fnd.functionName})."); return(builder); }
public SynIntrinsic(SynScope parent, string name, int byteSize) : base(parent, name, true) { this.byteSize = byteSize; }
public SynCanidateFuncs(SynScope scope) { this.scope = scope; }
public SynScope(SynScope parent) { this.parentScope = parent; }
public SynStruct(SynScope parentScope, string name) : base(parentScope, name, false) { }
public SynRegion(SynScope parent) : base(parent) { }
public SynParsedObj(SynScope parentScope) { this.parentScope = parentScope; }