public CodePosition GetTwoDimensionalCordinatesFor(int stringOffset) { V_1 = this.newLineOffsets.BinarySearch(stringOffset); if (V_1 >= 0) { if (!this.isZeroIndexed) { V_0 = new CodePosition(V_1 + 1, -1); } else { V_0 = new CodePosition(V_1, -1); } } else { V_2 = ~V_1; V_3 = 0; if (V_2 != 0) { V_3 = this.newLineOffsets.get_Item(V_2 - 1) + this.get_NewLine().get_Length(); } V_4 = stringOffset - V_3; if (!this.isZeroIndexed) { V_0 = new CodePosition(V_2 + 1, V_4 + 1); } else { V_0 = new CodePosition(V_2, V_4); } } return(V_0); }
public static TypeNode Consume(Parser parser) { CodePosition oldCursor = parser.Cursor; TypeDesc type = null; switch (parser.LookAhead().Type) { case TokenInfo.TokenType.L_BRACKET: type = parser.TryConsumer(NativeArrayType.Consume); break; case TokenInfo.TokenType.IDENTIFIER: type = parser.TryManyConsumers(new Func <Parser, TypeDesc>[] { TypeAppC.Consume, NameTypeC.Consume }); break; case TokenInfo.TokenType.FUNC_TYPE: type = parser.TryConsumer(FuncTypeC.Consume); break; default: type = parser.TryConsumer(AtomTypeC.Consume); break; } if (type == null) { throw new ParserError(new FailedConsumer(), parser.Cursor); } return(new TypeNode(type, oldCursor)); }
public void StatementBlockPosition() { Enviroment enviroment = new Enviroment(provider); var block = TokenParser.Parse("int 0f .", enviroment, CodePosition.GetExternal()); Assert.AreEqual(block.position, CodePosition.GetExternal()); }
/// <summary> /// 报告错误并把分析结果设为false /// </summary> protected virtual void ErrorE(CodePosition postion, string msgFormat, params string[] msgParams) { this.FileContext.Errorf(postion, msgFormat, msgParams); AnalyCorrect = false; //string msg = string.Format(msgFormat, msgParams); //error(postion, msg); }
private void AnalyType() { if (_IsExist) { return; } ContextImportUse contextiu = this.FileContext.ImportUseContext; string[] names = contextiu.GetArgSegementer().Cut(ArgText); if (names.Length != 2) { throw new CCException(); } ArgZTypeName = names[0]; ArgName = names[1]; if (this.MethodContext.HasParameter(ArgName)) { _IsExist = true; CodePosition argPos = new CodePosition(ParameterRaw.ParameterToken.Line, ParameterRaw.ParameterToken.Col + ArgZTypeName.Length); FileContext.Errorf(argPos, "参数'{0}'重复", ArgName); } else { ZType[] ztypes = contextiu.SearchZTypesByClassNameOrDimItem(ArgZTypeName); ArgZType = ztypes[0]; //_argSymbol = new SymbolArg(ArgName, ArgZType); //this.ProcContext.AddParameter(_argSymbol); //_argSymbol = new ZCParamInfo(ArgName, ArgZType); //ProcContext.AddParameter(_argSymbol); _ZCParam = MethodContext.AddParameterName(ArgName); _ZCParam.ZParamType = ArgZType; } }
public Token Eat(TokenInfo.TokenType tokenType, bool facultative = true) { if (LookAhead().Type != tokenType) { if (facultative) { throw new ParserError(new FailedEatToken(tokenType), Cursor); } else { throw new ParserError(new ExpectedTokenException(tokenType), Cursor); } } else if (Tokens.Length == 0) { throw new ParserError(new NoTokenLeft(), Cursor); } if (LookAhead().Type != TokenInfo.TokenType.EOL) { Cursor = LookAhead().Pos; } else { Cursor = new CodePosition(LookAhead().Pos.Line + 1, 1); } LookAheadIndex++; return(Tokens[LookAheadIndex - 1]); }
private static TypeDesc ResolveType(NativeArray arr, Environment env, CodePosition position) { ExprNode[] elements = ((NativeArray)arr).Elements; if (elements.Length == 0) { return(Utils.FreshTypeVariable()); } else { TypeDesc listType = ResolveType(elements[0], env, position); foreach (ExprNode elem in elements) { TypeDesc elemType = ResolveType(elem, env, position); if (elemType.GetType() != listType.GetType()) // Not sure, should use `is` { throw new TypecheckerError( new MismatchedTypes(listType, elemType), elem.Position ); } } return(listType); } }
static void Run(string code) { List <CodeTokenizer.Token> tokens = null; StatementBlock block = null; try { tokens = CodeTokenizer.Tokenize(code, file); var tokensEnum = (IEnumerator <CodeTokenizer.Token>)tokens.GetEnumerator(); block = TokenParser.Parse(ref tokensEnum, enviroment, CodePosition.GetExternal()); } catch (WordScriptException ex) { Console.WriteLine(ex.Message); } if (tokens != null) { foreach (var token in tokens) { Console.WriteLine(token.ToString()); } } Console.WriteLine(""); if (block != null) { foreach (var node in block.GetSyntaxNodes()) { Console.WriteLine(node.Debug()); } var ret = block.Evaluate(); Console.WriteLine(ret?.ToString() ?? "null"); } }
private static CodeSpan GetSpan(TwoDimensionalString twoDString, OffsetSpan position) { CodePosition start = twoDString.GetTwoDimensionalCordinatesFor(position.StartOffset); CodePosition end = twoDString.GetTwoDimensionalCordinatesFor(position.EndOffset); return(new CodeSpan(start, end)); }
public void AddEntry(Identifier id, TypeDesc type, CodePosition position) { if (Has(id)) { throw new TypecheckerError(new EnvironmentError($"Name `{id}` is already defined"), position); } table.Add(id, type); }
public TypeDesc LookupId(Identifier id, CodePosition position) { if (!Has(id)) { throw new TypecheckerError(new EnvironmentError($"Unknown name `{id}`"), position); } return(table[id]); }
public void ReturningFromBlock() { Enviroment enviroment = new Enviroment(provider); var block = TokenParser.Parse("return 10 .", enviroment, CodePosition.GetExternal()); block.Validate(enviroment); Assert.AreEqual(block.ReturnType, typeof(int)); }
public CodeBlock BlockTo(CodePosition other) { if (Line < other.Line || (Line == other.Line && Column < other.Column)) { return(new CodeBlock(this, other)); } return(new CodeBlock(other, this)); }
public static ExprNode Consume(Parser parser) { CodePosition oldCursor = parser.Cursor; return(new ExprNode( parser.TryConsumer((Parser p) => ParseBinOpSeq(p)), oldCursor )); }
public void VariableRedefinitionFail() { Enviroment enviroment = new Enviroment(TypeInfoProvider.GetGlobal()); enviroment.StartBlock(CodePosition.GetExternal()); enviroment.DefineVariable("x", typeof(int), CodePosition.GetExternal()); enviroment.DefineVariable("x", typeof(int), CodePosition.GetExternal()); enviroment.EndBlock(); }
public override void WriteReference(string value, object reference) { CodePosition startPosition = new CodePosition(this.CurrentLineNumber, this.CurrentColumnIndex); base.WriteReference(value, reference); CodePosition endPosition = new CodePosition(this.CurrentLineNumber, this.CurrentColumnIndex); this.CodeSpanToMemberReference.Add(new CodeSpan(startPosition, endPosition), reference as MemberReference); }
public void VariableTypeSafetyInCode() { Enviroment enviroment = new Enviroment(TypeInfoProvider.GetGlobal()); enviroment.StartBlock(CodePosition.GetExternal()); enviroment.DefineVariable("x", typeof(string), CodePosition.GetExternal()); TokenParser.Parse("x= 0 .", enviroment, CodePosition.GetExternal()); enviroment.EndBlock(); }
public static TypeDesc ResolveType(ExprNode expr, Environment env, CodePosition position) { Expr value = expr.Value; if (value is BooleanLit) { return(new BoolType()); } if (value is CharLit) { return(new CharType()); } if (value is DoubleLit) { return(new DoubleType()); } if (value is IntegerLit) { return(new IntType()); } if (value is StringLit) { return(new StringType()); } if (value is NullExpr) { return(new NullType()); } if (value is Identifier) { return(env.LookupId((Identifier)value, position)); } if (value is NativeArray) { return(ResolveType((NativeArray)value, env, position)); } if (value is Instanciation) { return(ResolveType((Instanciation)value, env, position)); } if (value is IfInstr) { return(ResolveType((IfInstr)value, env, position)); } if (value is FuncCall) { return(ResolveType((FuncCall)value, env, position)); } return(new VoidType()); throw new NotImplementedException(); }
public void VariableDefinition() { Enviroment enviroment = new Enviroment(TypeInfoProvider.GetGlobal()); enviroment.StartBlock(CodePosition.GetExternal()); Assert.IsNull(enviroment.GetVariable("y")); enviroment.DefineVariable("x", typeof(int), CodePosition.GetExternal()); Assert.IsNotNull(enviroment.GetVariable("x")); enviroment.EndBlock(); }
public void TypeAttribute() { Enviroment enviroment = new Enviroment(provider); Assert.IsNotNull(TokenParser.Parse("TestMapClass .", enviroment, CodePosition.GetExternal()).Evaluate() as TestMapClass); Assert.AreEqual(5, TokenParser.Parse("TestMapClass , .i .", enviroment, CodePosition.GetExternal()).Evaluate()); Assert.AreEqual(20, TokenParser.Parse("TestMapClass 20 , .i .", enviroment, CodePosition.GetExternal()).Evaluate()); Assert.AreEqual(20, TokenParser.Parse("TestMapClass 20 , .GetI .", enviroment, CodePosition.GetExternal()).Evaluate()); }
public void RunTheExampleFile() { var text = System.IO.File.ReadAllText("../../../Examples/example.ws"); Enviroment enviroment = new Enviroment(TypeInfoProvider.GetGlobal()); var block = TokenParser.Parse(text, enviroment, CodePosition.GetExternal()); block.Validate(enviroment); Assert.AreEqual(typeof(string), block.ReturnType); Assert.AreEqual("aaaab", block.Evaluate()); }
public static InstrNode Consume(Parser parser) { CodePosition oldCursor = parser.Cursor; Instr instr; switch (parser.LookAhead().Type) { case TokenInfo.TokenType.EOL: case TokenInfo.TokenType.L_CURLY_BRACKET: instr = parser.TryConsumer((Parser p) => Block.Consume(p)); break; case TokenInfo.TokenType.WHILE: instr = parser.TryConsumer(WhileLoop.Consume); break; case TokenInfo.TokenType.FOR: instr = parser.TryConsumer(ForLoop.Consume); break; case TokenInfo.TokenType.CONST: case TokenInfo.TokenType.VAR: instr = parser.TryConsumer((Parser p) => VarDecl.Consume(p)); break; case TokenInfo.TokenType.RETURN: instr = parser.TryConsumer(ReturnInstr.Consume); break; case TokenInfo.TokenType.BREAK: instr = parser.TryConsumer(BreakInstr.Consume); break; case TokenInfo.TokenType.CONTINUE: instr = parser.TryConsumer(ContinueInstr.Consume); break; default: instr = parser.TryManyConsumers(new Func <Parser, Instr>[] { VarMutation.Consume, ExprInstr.Consume }); break; } if (instr == null) { throw new ParserError(new FailedConsumer(), parser.Cursor); } return(new InstrNode(instr, oldCursor)); }
public void VariablesInCode() { Enviroment enviroment = new Enviroment(TypeInfoProvider.GetGlobal()); enviroment.StartBlock(CodePosition.GetExternal()); enviroment.DefineVariable("x", typeof(int), CodePosition.GetExternal()); TokenParser.Parse("DEFINE:y:int . &y . y= 0 . &x . x= &y .", enviroment, CodePosition.GetExternal(), true); Assert.IsNotNull(enviroment.GetVariable("y")); Assert.AreEqual(enviroment.GetVariable("y").Type, typeof(int)); enviroment.EndBlock(); }
private static TypeDesc ResolveType(IfInstr ifinstr, Environment env, CodePosition position) { TypeDesc conditionType = ResolveType(ifinstr.Condition, env, ifinstr.Condition.Position); ExprNode mainBranchLast; ExprNode elseBranchLast; TypeDesc mainBranchType; TypeDesc elseBranchType; if (!(conditionType is BoolType)) { throw new TypecheckerError( new MismatchedTypes(new BoolType(), conditionType), ifinstr.Condition.Position ); } RegularChecker mainChecker = new RegularChecker(ifinstr.MainBranch, env); RegularChecker elseChecker = new RegularChecker(ifinstr.ElseBranch, env); mainChecker.Typecheck(); elseChecker.Typecheck(); try { mainBranchLast = Utils.GetBlockLast(ifinstr.MainBranch); } catch (ArgumentException) { mainBranchLast = new ExprNode(new NullExpr(), ifinstr.Condition.Position); } try { elseBranchLast = Utils.GetBlockLast(ifinstr.ElseBranch); } catch (ArgumentException) { elseBranchLast = new ExprNode(new NullExpr(), ifinstr.Condition.Position); } mainBranchType = ResolveType(mainBranchLast, env, position); elseBranchType = ResolveType(elseBranchLast, env, position); if (mainBranchType.GetType() != elseBranchType.GetType()) { throw new TypecheckerError( new MismatchedTypes(mainBranchType, elseBranchType), position ); } return(mainBranchType); }
private StmtBlock ParseStmtBlock(CodePosition startPosition) { StmtBlock stmtBlock = new StmtBlock(); while (tape.Current.Col > startPosition.Col) { Stmt stmt = ParseStmt(); if (stmt != null) { stmtBlock.StmtList.Add(stmt); } } return(stmtBlock); }
private bool LoadPackageTypes(string PackageFullName, CodePosition position) { var fileContext = this.ASTFile.FileContext; ZPackageInfo packageDesc = fileContext.ProjectContext.SearchZPackageDesc(PackageFullName); if (packageDesc == null) { this.ASTFile.FileContext.Errorf(position, "不存在'{0}'开发包", PackageFullName); return(false); } else { this.AddPackage(packageDesc); } return(true); }
protected Exp AnalyCondition(Exp condition, CodePosition nullPosition) { if (condition == null) { ErrorE(nullPosition, "条件表达式不是判断表达式"); return(null); } condition = ParseExp(condition); condition = condition.Analy(); if (condition.AnalyCorrect) { if (condition.RetType != ZTypeManager.ZBOOL) { ErrorE(condition.Postion, "条件表达式不是判断表达式"); } } return(condition); }
private Stmt ParseIf() { StmtIf ifStmt = new StmtIf(); ifStmt.IfToken = tape.Current; StmtIf.StmtIfTrue ifPart = ParseTruePart(); ifStmt.Parts.Add(ifPart); while (tape.CurrentKind == TokenKind.ELSEIF) { StmtIf.StmtIfTrue elseifPart = ParseTruePart(); ifStmt.Parts.Add(elseifPart); } if (tape.CurrentKind == TokenKind.ELSE) { CodePosition pos = tape.Current.Position; tape.MoveNext(); ifStmt.ElsePart = ParseStmtBlock(pos); } return(ifStmt); }
public void VariableInheritance() { Enviroment enviroment = new Enviroment(TypeInfoProvider.GetGlobal()); enviroment.StartBlock(CodePosition.GetExternal()); var xOut = enviroment.DefineVariable("x", typeof(string), CodePosition.GetExternal()); var yOut = enviroment.DefineVariable("y", typeof(int), CodePosition.GetExternal()); enviroment.StartBlock(CodePosition.GetExternal()); Assert.AreEqual(yOut, enviroment.GetVariable("y")); Assert.AreEqual(xOut, enviroment.GetVariable("x")); var yInner = enviroment.DefineVariable("y", typeof(string), CodePosition.GetExternal()); var zInner = enviroment.DefineVariable("z", typeof(string), CodePosition.GetExternal()); Assert.AreNotEqual(yOut, enviroment.GetVariable("y")); enviroment.EndBlock(); Assert.AreEqual(yOut, enviroment.GetVariable("y")); Assert.IsNull(enviroment.GetVariable("z")); enviroment.EndBlock(); }
public CodePosition GetTwoDimensionalCordinatesFor(int stringOffset) { CodePosition result; int line = newLineOffsets.BinarySearch(stringOffset); if (line < 0) // there is no newline string at this offset { int lineNumber = ~line; int lineStartOffset = 0; if (lineNumber != 0) { lineStartOffset = newLineOffsets[lineNumber - 1] + NewLine.Length; } int column = stringOffset - lineStartOffset; if (isZeroIndexed) { result = new CodePosition(lineNumber, column); } else { result = new CodePosition(lineNumber + 1, column + 1); //VS editor uses [1,1] as origin as opposed to [0,0] } } else { if (isZeroIndexed) { result = new CodePosition(line, -1); } else { result = new CodePosition(line + 1, -1); } } return(result); }
public CodePosition GetPosition() { var p = new CodePosition(); //Offset within the code block for (int n = 0; n < offset; n++) { switch (code[n]) { case '\r': continue; //Assume we have a '\n' in pair case '\n': p.Line += 1; p.Column = 0; break; default: p.Column += 1; break; } } //Add code block offset p += statement.CodePosition; return p; }
public override Expr Visit (Expr e) { Instruction inst; if (this.instructionLookup.TryGetValue (e, out inst)) { var seq = inst.SequencePoint; if (seq != null) { this.SourceCodeFileName = seq.Document.Url; var instStart = new CodePosition(seq.StartLine, seq.StartColumn); if (this.StartPosition.IsEmpty || instStart < this.StartPosition) { this.StartPosition = instStart; } var instEnd = new CodePosition (seq.EndLine, seq.EndColumn); if (this.EndPosition.IsEmpty || instEnd > this.EndPosition) { this.EndPosition = instEnd; } } } return base.Visit (e); }
/// <summary> /// Initializes a new instance of the <see cref="CompilationError"/> class with the specified program posistion and message. /// </summary> /// <param name="position">The position of the program this exception is being thrown.</param> /// <param name="message">The exception message describing the error.</param> public CompilationError(CodePosition position, string message) { Position = position; Message = message; }
public SyntaxError(CodePosition pos, string message) : this(new CodeRange(pos), message) { }
/// <summary> /// Initializes a new instance of the <see cref="CompilationError"/> class with the specified program posistion. /// </summary> /// <param name="position">The position of the program this exception is being thrown.</param> public CompilationError(CodePosition position) { Position = position; }