private AstItem ReadNewPointer(TokenIterator tokens) { tokens.PopExpected(TokenKind.New); var token = tokens.Current(); if (token.Kind == TokenKind.SquareBracketOpen) { return(AstItem.NewHeapArray(ReadArray(tokens))); } else { var subType = ReadDataType(tokens); var dataType = DataType.Pointer(subType); var amountExpression = AstItem.Immediate("1"); var current = tokens.Current(); if (current != null && current.Kind == TokenKind.RoundBracketOpen) { tokens.Pop(); amountExpression = ReadExpression(tokens, TokenKind.RoundBracketClose); tokens.PopExpected(TokenKind.RoundBracketClose); } return(AstItem.NewRawPointer(dataType, amountExpression)); } }
private AstItem ReadForLoop(TokenIterator tokens) { //for i in 0..5 [inc 1] {...} tokens.PopExpected(TokenKind.For); var varName = tokens.PopExpected(TokenKind.Word); tokens.PopExpected(TokenKind.In); var startExpression = ReadExpression(tokens, TokenKind.To); tokens.PopExpected(TokenKind.To); var endExpression = ReadExpression(tokens, new List <TokenKind>() { TokenKind.CurlyBracketOpen, TokenKind.Inc }); var next = tokens.Pop(); AstItem incExpression = AstItem.Immediate("1"); if (next.Kind == TokenKind.Inc) { incExpression = ReadExpression(tokens, TokenKind.CurlyBracketOpen); tokens.PopExpected(TokenKind.CurlyBracketOpen); } else { Assert.True(next.Kind == TokenKind.CurlyBracketOpen, "Expected 'inc' or '{', got: " + next); } var statements = ReadStatements(tokens); tokens.PopExpected(TokenKind.CurlyBracketClose); return(AstItem.ForLoop(varName.Value, startExpression, endExpression, incExpression, statements)); }
private AstItem ReadSingleAstItem(TokenIterator tokens) { AstItem result; var token = tokens.Current(); if (token.Kind == TokenKind.Word) { var next = tokens.Next(); if (next != null) { if (next.Kind == TokenKind.RoundBracketOpen) { var dataType = DataType.FindByName(token.Value); if (dataType != null && dataType.IsVector) { //Vector construction with specific vector type name, i.e. "vec4f(...)" result = ReadVector(tokens); tokens.StepBack(); } else { result = ReadFuncCall(tokens); tokens.StepBack(); } } else if (next.Kind == TokenKind.SquareBracketOpen) { result = ReadIndexAccess(tokens); tokens.StepBack(); } else { //Not sure what identifier is here. Could be type name, variable etc. Will be specified in next steps. result = AstItem.AsIdentifier(token.Value); } } else { //Not sure what identifier is here. Could be type name, variable etc. Will be specified in next steps. result = AstItem.AsIdentifier(token.Value); } } else if (token.Kind == TokenKind.VectorConstructor) { //Vector construction with the generic "vec(...)" result = ReadVector(tokens); tokens.StepBack(); } else if (token.Kind == TokenKind.Number) { result = AstItem.Immediate(token.Value); } else if (token.Kind == TokenKind.True || token.Kind == TokenKind.False) { result = AstItem.Immediate(DataType.BOOL, token.Value); } else if (token.Kind == TokenKind.Char) { result = AstItem.Immediate(DataType.CHAR8, token.Value); } else if (token.Kind == TokenKind.String) { result = AstItem.Immediate(DataType.STRING8, token.Value); } else if (token.Kind == TokenKind.SquareBracketOpen) { result = AstItem.NewStackArray(ReadArray(tokens)); } else { throw new Exception("Unexpected token type in expression: " + token); } return(result); }