...
This commit is contained in:
@@ -417,7 +417,7 @@ public sealed class Parser
|
||||
case Symbol.Pipe:
|
||||
binaryExpressionOperator = BinaryOperatorSyntax.BitwiseOr;
|
||||
return true;
|
||||
case Symbol.XOr:
|
||||
case Symbol.Tilde:
|
||||
binaryExpressionOperator = BinaryOperatorSyntax.BitwiseXor;
|
||||
return true;
|
||||
default:
|
||||
@@ -439,12 +439,12 @@ public sealed class Parser
|
||||
IdentifierToken identifier => ParseIdentifier(startIndex, identifier),
|
||||
SymbolToken symbolToken => symbolToken.Symbol switch
|
||||
{
|
||||
Symbol.Ampersand => new AddressOfSyntax(GetTokens(startIndex), ParsePrimaryExpression()),
|
||||
Symbol.Ampersand => ParseAddressOf(startIndex),
|
||||
Symbol.OpenParen => ParseParenthesizedExpression(),
|
||||
Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()),
|
||||
Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()),
|
||||
Symbol.Minus => ParseUnaryNegate(startIndex),
|
||||
Symbol.Bang => ParseUnaryInvert(startIndex),
|
||||
Symbol.OpenBracket => ParseArrayInitializer(startIndex),
|
||||
Symbol.OpenBrace => new StructInitializerSyntax(GetTokens(startIndex), null, ParseStructInitializerBody()),
|
||||
Symbol.OpenBrace => ParseUnnamedStructInitializer(startIndex),
|
||||
Symbol.Struct => ParseStructInitializer(startIndex),
|
||||
Symbol.At => ParseBuiltinFunction(startIndex),
|
||||
_ => throw new CompileException(Diagnostic
|
||||
@@ -463,6 +463,24 @@ public sealed class Parser
|
||||
return ParsePostfixOperators(expr);
|
||||
}
|
||||
|
||||
private AddressOfSyntax ParseAddressOf(int startIndex)
|
||||
{
|
||||
var expression = ParsePrimaryExpression();
|
||||
return new AddressOfSyntax(GetTokens(startIndex), expression);
|
||||
}
|
||||
|
||||
private UnaryExpressionSyntax ParseUnaryInvert(int startIndex)
|
||||
{
|
||||
var expression = ParsePrimaryExpression();
|
||||
return new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, expression);
|
||||
}
|
||||
|
||||
private UnaryExpressionSyntax ParseUnaryNegate(int startIndex)
|
||||
{
|
||||
var expression = ParsePrimaryExpression();
|
||||
return new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, expression);
|
||||
}
|
||||
|
||||
private ExpressionSyntax ParseBuiltinFunction(int startIndex)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
@@ -587,6 +605,12 @@ public sealed class Parser
|
||||
return new StructInitializerSyntax(GetTokens(startIndex), type, initializers);
|
||||
}
|
||||
|
||||
private StructInitializerSyntax ParseUnnamedStructInitializer(int startIndex)
|
||||
{
|
||||
var body = ParseStructInitializerBody();
|
||||
return new StructInitializerSyntax(GetTokens(startIndex), null, body);
|
||||
}
|
||||
|
||||
private Dictionary<IdentifierToken, ExpressionSyntax> ParseStructInitializerBody()
|
||||
{
|
||||
Dictionary<IdentifierToken, ExpressionSyntax> initializers = [];
|
||||
|
||||
@@ -121,6 +121,7 @@ public enum Symbol
|
||||
Star,
|
||||
ForwardSlash,
|
||||
Caret,
|
||||
Tilde,
|
||||
Ampersand,
|
||||
Semi,
|
||||
Percent,
|
||||
@@ -129,7 +130,6 @@ public enum Symbol
|
||||
Pipe,
|
||||
And,
|
||||
Or,
|
||||
XOr,
|
||||
At,
|
||||
QuestionMark,
|
||||
}
|
||||
@@ -189,6 +189,7 @@ public record SymbolToken(SourceSpan Span, Symbol Symbol) : Token(Span)
|
||||
Symbol.Pipe => "|",
|
||||
Symbol.At => "@",
|
||||
Symbol.QuestionMark => "?",
|
||||
Symbol.Tilde => "~",
|
||||
_ => "none",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,20 +11,20 @@ public sealed class Tokenizer
|
||||
private int _column = 1;
|
||||
|
||||
public List<Diagnostic> Diagnostics { get; set; } = new(16);
|
||||
public List<Token> Tokens { get; set; } = new(256);
|
||||
|
||||
public void Tokenize(string fileName, string content)
|
||||
public List<Token> Tokenize(string fileName, string content)
|
||||
{
|
||||
_fileName = fileName;
|
||||
_content = content;
|
||||
|
||||
Diagnostics = [];
|
||||
Tokens = [];
|
||||
|
||||
_index = 0;
|
||||
_line = 1;
|
||||
_column = 1;
|
||||
|
||||
var tokens = new List<Token>();
|
||||
|
||||
while (_index < _content.Length)
|
||||
{
|
||||
try
|
||||
@@ -54,7 +54,7 @@ public sealed class Tokenizer
|
||||
continue;
|
||||
}
|
||||
|
||||
Tokens.Add(ParseToken(current, _line, _column));
|
||||
tokens.Add(ParseToken(current, _line, _column));
|
||||
}
|
||||
catch (CompileException e)
|
||||
{
|
||||
@@ -62,6 +62,8 @@ public sealed class Tokenizer
|
||||
Next();
|
||||
}
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private Token ParseToken(char current, int lineStart, int columnStart)
|
||||
@@ -295,7 +297,6 @@ public sealed class Tokenizer
|
||||
"&&" => Symbol.And,
|
||||
"||" => Symbol.Or,
|
||||
"::" => Symbol.DoubleColon,
|
||||
"x|" => Symbol.XOr,
|
||||
_ => Symbol.None
|
||||
},
|
||||
1 => span[0] switch
|
||||
@@ -324,6 +325,7 @@ public sealed class Tokenizer
|
||||
'|' => Symbol.Pipe,
|
||||
'@' => Symbol.At,
|
||||
'?' => Symbol.QuestionMark,
|
||||
'~' => Symbol.Tilde,
|
||||
_ => Symbol.None
|
||||
},
|
||||
_ => Symbol.None
|
||||
|
||||
Reference in New Issue
Block a user