public override void BreakApartParsedTokens() { SynLog.Log($"Breaking apart type {this.typeName}."); SynLog.LogFragments(this.declarationTokens); // For struct structname { ... }, remove everything except the ... this.declarationTokens.RemoveRange(0, 3); int lastIdx = this.declarationTokens.Count - 1; if (this.declarationTokens[lastIdx].MatchesSymbol(";")) { this.declarationTokens.RemoveRange(lastIdx - 1, 2); } else { this.declarationTokens.RemoveRange(lastIdx, 1); } while (this.declarationTokens.Count > 0) { if (this.declarationTokens[0].MatchesSymbol(";") == true) { this.declarationTokens.RemoveAt(0); continue; } SynRegion rgn = SynRegion.Parse(this, this.declarationTokens); if (rgn != null) { this.regions.Add(rgn.name, rgn); continue; } SynFuncDecl fnParse = SynFuncDecl.Parse( this, this.declarationTokens, this.typeName, false, SynFuncDecl.ParseType.StructContext); if (fnParse != null) { this.AddFunction(fnParse); continue; } SynVarValue varParse = SynVarValue.ParseBodyVar(this.declarationTokens, SynVarValue.OuterScope.Struct); if (varParse != null) { this.AddVariable(varParse); continue; } } }
// 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); }