public virtual void analysis() { int startIndex = 0; for (int i = 0; i < code.Length; i++) { startIndex = i; CodeSymbol curSymbol = PBFile.FindNextSymbol(code, startIndex); int Index = curSymbol.Index; if (Index > 0) { string key = code.Substring(startIndex, Index - startIndex); if (key == "") { } else { //关键字压入 codekeyStack.Add(new CodeKeyword(startIndex, key)); } } else if (Index == -1) { break; } i = Index; } }
public InterOp CompileAssignExpression(SyntaxTreeNode node) { CodeSymbol symbol = ToIntermediateExpression(node[0]) as CodeSymbol; InterCopy copy; CodeValue source; if (node[2].ValueString == "None") { source = ToIntermediateExpression(node[1]); } else { var i = new InterBinOp(Operator.FromName(node[2].ValueString), symbol, ToIntermediateExpression(node[1])); source = new InterOpValue(i, builder.CurrentMethod); } if (symbol == null) { copy = new InterCopy(new LateReferenceResolver(node[0], builder.CurrentType.NamespaceContext), source); } else { copy = new InterCopy(symbol, source); } copy.SetOwner(builder.CurrentMethod); return(copy); }
// Return a CodeSymbol with the supplied values. private static CodeSymbol GenerateSymbol(string name, int address) { CodeSymbol thisSymbol = new CodeSymbol(); thisSymbol.symbol = name; thisSymbol.location = address; return(thisSymbol); }
public void CompileVarDec(TypeName type, SyntaxTreeNode node) { string name = node[0].ValueString; if (CurrentTable.Contains(name)) { ErrorManager.ExitWithError(new Exception("Duplicate ID: " + name)); } CodeSymbol symbol = builder.AddLocal(name, type);//CurrentTable.AddSymbol(new CodeSymbol(name, node[2].ValueString)); if (node.Children.Length > 1) { builder.AddInstruction(new InterCopy(symbol, ToIntermediateExpression(node[1]))); } }
private static void Reconstruct(CodeSymbol c, int index, BaseNode parent) { if (index == c.Code.Length - 1) { if (c.Code[index] == LeftValue) { if (parent.Left != null) { throw new Exception("Bad code"); } parent.Left = new BaseLeafNode { V = c.Symbol }; } else { if (parent.Right != null) { throw new Exception("Bad code"); } parent.Right = new BaseLeafNode { V = c.Symbol }; } } else { if (c.Code[index] == LeftValue) { parent.Left = parent.Left != null ? parent.Left : new BaseNode(); if (parent.Left is IEncodingLeafNode) { throw new Exception("Bad code"); } Reconstruct(c, index + 1, (BaseNode)parent.Left); } else { parent.Right = parent.Right != null ? parent.Right : new BaseNode(); if (parent.Right is IEncodingLeafNode) { throw new Exception("Bad code"); } Reconstruct(c, index + 1, (BaseNode)parent.Right); } } }
//TODO: Add back pre-emit type conversion public override void Emit(IlBuilder builder) { base.Emit(builder); if (!Owner.IsStatic && FieldOrPropertySymbol.IsFieldOrProperty(_target)) { var forp = FieldOrPropertySymbol.ToFieldOrPropertySymbol(_target); if (!forp.HasOwner()) { forp.SetOwner(Owner.ThisPointer); _target = forp; } } _target.Store(builder, _source); //builder.EmitOpCode(_target.Location.GetStoreOpcode(), "For ID " + _target.ID); }
public override void Bind(IntermediateBuilder context) { _source.Bind(context); if (_target == null) { _resolver.SetOwner(Owner); _resolver.CanBeType = false; _resolver.Bind(context); Debug.Assert(_resolver.IsFieldOrProperty); _target = _resolver.GetReferencedFieldOrProperty(); } else { _target.Bind(context); } if (_source.Type.CanAssignTo(_target.Type) != AssignType.CanAssign) { _source = new ConvertedValue(_source, _target.Type, Owner); } }
public void read() { List <CodeKeyword> codekeyStack = new List <CodeKeyword>(); List <CodeSymbol> symbolStack = new List <CodeSymbol>(); List <PBElement> elementStack = new List <PBElement>(); elementStack.Add(this); if (charArr == null) { charArr = code.ToCharArray(); } PBElement curPBElement = null; int startIndex = 0; for (int i = 0; i < code.Length; i++) { startIndex = i; CodeSymbol curSymbol = FindNextSymbol(code, startIndex); int Index = curSymbol.Index; if (Index > 0) { //跳过注释 if (curSymbol.Symbol == '/') { if (charArr [curSymbol.Index + 1] == '*') { while (Index < charArr.Length) { curSymbol = FindNextSymbol(code, Index + 1, '/'); Index = curSymbol.Index; if (Index == -1) { Debug.LogErrorFormat("{0} not find '/' end", name); return; } if (charArr [curSymbol.Index - 1] == '*') { break; } } i = Index; Debug.LogWarningFormat("{0} jump /**/ index {1} ", name, Index); continue; } } string key = code.Substring(startIndex, Index - startIndex); if (curPBElement != null && key.StartsWith("//")) { // curPBElement.Notes = key; } if (key == "") { } else { //关键字压入 codekeyStack.Add(new CodeKeyword(startIndex, key)); } if (key.Equals("import")) { //PBFile pbFile = new PBFile (); PBImportFile pushPBImportFile = new PBImportFile(); pushPBImportFile.startIndex = startIndex; elementStack.Add(pushPBImportFile); curPBElement = pushPBImportFile; } else if (key.Equals("package")) { PBPackage pushPBPackage = new PBPackage(); pushPBPackage.startIndex = startIndex; elementStack.Add(pushPBPackage); curPBElement = pushPBPackage; } else if (key.Equals("required") || key.Equals("optional") || key.Equals("repeated")) { PBAttribute pushPBAttribute = new PBAttribute(); pushPBAttribute.startIndex = startIndex; elementStack.Add(pushPBAttribute); curPBElement = pushPBAttribute; } else //enum压入 if (key == "enum") { PBEnum pushPBElement = new PBEnum(); pushPBElement.pbFile = this; pushPBElement.name = key; pushPBElement.startIndex = Index; elementStack.Add(pushPBElement); curPBElement = pushPBElement; } else //message压入 if (key == "message") { PBMessage pushPBMessage = new PBMessage(); pushPBMessage.pbFile = this; pushPBMessage.name = key; pushPBMessage.startIndex = Index; elementStack.Add(pushPBMessage); curPBElement = pushPBMessage; if (codekeyStack.Count >= 2 && codekeyStack[codekeyStack.Count - 2].value.StartsWith("//")) { pushPBMessage.Notes = codekeyStack[codekeyStack.Count - 2].value; } } /* * if(curSymbol.Symbol == '='){ * string pname = (string)codekeyStack[codekeyStack.Count-1]; * Debug.LogWarning(" >> pname :))))) --------> " + pname); * }else */ if (curSymbol.Symbol == '{') { CodeKeyword ptype = codekeyStack[codekeyStack.Count - 2]; CodeKeyword pname = codekeyStack[codekeyStack.Count - 1]; //Debug.LogWarning(" >> Index :))))) --------> " + pname.Index + ", >> pname :))))) --------> " + pname.value ); //压入符号栈 symbolStack.Add(curSymbol); } else if (curSymbol.Symbol == '}') { //块结束 CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1]; if (pushSymbol.Symbol == '{') { symbolStack.RemoveAt(symbolStack.Count - 1); //弹出符号栈 PBElement popUpPBElement = elementStack[elementStack.Count - 1]; popUpPBElement.endIndex = Index; popUpPBElement.cut(code); elementStack.RemoveAt(elementStack.Count - 1); if (elementStack.Count > 0) { elementStack[elementStack.Count - 1].addChild(popUpPBElement); } } else { Debug.LogError("Symbol Error { "); } } else if (curSymbol.Symbol == '[') { symbolStack.Add(curSymbol); PBKeyValue pushPBKeyValue = new PBKeyValue(); pushPBKeyValue.startIndex = Index; elementStack.Add(pushPBKeyValue); } else if (curSymbol.Symbol == ']') { CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1]; if (pushSymbol.Symbol == '[') { symbolStack.RemoveAt(symbolStack.Count - 1); //弹出 PBElement popUpPBElement = elementStack[elementStack.Count - 1]; popUpPBElement.endIndex = Index; popUpPBElement.cut(code); elementStack.RemoveAt(elementStack.Count - 1); if (elementStack.Count > 0) { elementStack[elementStack.Count - 1].addChild(popUpPBElement); } } else { Debug.LogError("Symbol Error [ "); } } else if (curSymbol.Symbol == ';') { //弹出 PBElement popUpPBElement = elementStack[elementStack.Count - 1]; if (popUpPBElement.pb_type == PBElement.type_PBPackage) { popUpPBElement.endIndex = Index; popUpPBElement.cut(code); this.package = (popUpPBElement as PBPackage).pbPackageName; elementStack.RemoveAt(elementStack.Count - 1); } else if (popUpPBElement.pb_type == PBElement.type_PBImportFile) { popUpPBElement.endIndex = Index; popUpPBElement.cut(code); PBFile pbFile = new PBFile(); pbFile.name = (popUpPBElement as PBImportFile).pbFilePath; pbFile.readFile(pbFile.name); pbFile.read(); PBFileImportDic.Add(pbFile.name, pbFile); elementStack.RemoveAt(elementStack.Count - 1); } else //枚举 属性 if (popUpPBElement.pb_type == PBElement.type_PBEnum) { PBKeyValue PBKeyValue = new PBKeyValue(); PBKeyValue.key = codekeyStack[codekeyStack.Count - 2].value; PBKeyValue.value = codekeyStack[codekeyStack.Count - 1].value; PBKeyValue.startIndex = codekeyStack[codekeyStack.Count - 2].Index; PBKeyValue.endIndex = Index; PBKeyValue.cut(code); popUpPBElement.addChild(PBKeyValue); curPBElement = PBKeyValue; codekeyStack.RemoveAt(codekeyStack.Count - 1); codekeyStack.RemoveAt(codekeyStack.Count - 1); CodeSymbol nextSymbol = FindNextSymbol(code, Index + 1); string nextKey = code.Substring(Index + 1, nextSymbol.Index - (Index + 1)); if (curPBElement != null && nextKey.StartsWith("//")) { curPBElement.Notes = nextKey; } } else //message 属性 if (popUpPBElement.pb_type == PBElement.type_PBAttribute) { popUpPBElement.endIndex = Index; popUpPBElement.cut(code); elementStack.RemoveAt(elementStack.Count - 1); curPBElement = popUpPBElement; if (elementStack.Count > 0) { elementStack[elementStack.Count - 1].addChild(popUpPBElement); } CodeSymbol nextSymbol = PBFile.FindNextSymbol(code, Index + 1); string nextKey = code.Substring(Index + 1, nextSymbol.Index - (Index + 1)); if (curPBElement != null && nextKey.StartsWith("//")) { curPBElement.Notes = nextKey; } } } } i = Index; } //getAllChild(this); }
public void PushAddress(CodeSymbol sym) { sym.PushAddress(this); }
public InterCopy(CodeSymbol target, CodeValue source) { _target = target; _source = source; }