Put + operator inbetween

This commit is contained in:
Eugen Wissner 2022-06-14 23:06:02 +02:00
parent fbbabe115f
commit 799c3fd89c
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
6 changed files with 45 additions and 38 deletions

View File

@ -14,7 +14,7 @@ struct Token
enum Type
{
number,
subroutine, // Operator.
operator,
let,
identifier,
equals,
@ -236,7 +236,7 @@ Array!Token lex(char[] buffer) @nogc
}
else if (source.front == '+') // Multi-character, random special characters.
{
tokens.insertBack(Token(Token.Type.subroutine, source.position));
tokens.insertBack(Token(Token.Type.operator, source.position));
source.popFront;
}
else if (source.front == '\n')

View File

@ -59,46 +59,22 @@ class Subroutine : Expression
Expression lhs, rhs;
}
private Result!Expression parseExpression(ref Array!(Token).Range tokens) @nogc
in (!tokens.empty, "Expected expression, got end of stream")
private Result!Expression parseFactor(ref Array!Token.Range tokens) @nogc
in (!tokens.empty, "Expected factor, got end of stream")
{
if (tokens.front.ofType(Token.Type.number))
{
auto number = MmapPool.instance.make!Number;
number.value = tokens.front.value!(Token.Type.number);
tokens.popFront;
return Result!Expression(number);
}
else if (tokens.front.ofType(Token.Type.identifier))
if (tokens.front.ofType(Token.Type.identifier))
{
auto variable = MmapPool.instance.make!Variable;
variable.identifier = tokens.front.value!(Token.Type.identifier);
tokens.popFront;
return Result!Expression(variable);
}
else if (tokens.front.ofType(Token.Type.subroutine))
else if (tokens.front.ofType(Token.Type.number))
{
auto subroutine = MmapPool.instance.make!Subroutine;
auto number = MmapPool.instance.make!Number;
number.value = tokens.front.value!(Token.Type.number);
tokens.popFront;
auto expression = parseExpression(tokens);
if (expression.valid)
{
subroutine.lhs = expression.result;
}
else
{
return Result!Expression("Expected left-hand side to be an expression", tokens.front.position);
}
expression = parseExpression(tokens);
if (expression.valid)
{
subroutine.rhs = expression.result;
}
else
{
return Result!Expression("Expected left-hand side to be an expression", tokens.front.position);
}
return Result!Expression(subroutine);
return Result!Expression(number);
}
else if (tokens.front.ofType(Token.Type.leftParen))
{
@ -109,7 +85,38 @@ in (!tokens.empty, "Expected expression, got end of stream")
tokens.popFront;
return expression;
}
return Result!Expression("Expected an expression", tokens.front.position);
return Result!Expression("Expected a factor", tokens.front.position);
}
private Result!Expression parseTerm(ref Array!(Token).Range tokens) @nogc
{
return parseFactor(tokens);
}
private Result!Expression parseExpression(ref Array!(Token).Range tokens) @nogc
in (!tokens.empty, "Expected expression, got end of stream")
{
auto term = parseTerm(tokens);
if (!term.valid || tokens.empty || !tokens.front.ofType(Token.Type.operator))
{
return term;
}
tokens.popFront;
auto operator = MmapPool.instance.make!Subroutine;
auto expression = parseExpression(tokens);
if (expression.valid)
{
operator.lhs = term.result;
operator.rhs = expression.result;
return Result!Expression(operator);
}
else
{
return Result!Expression("Expected right-hand side to be an expression", tokens.front.position);
}
}
private Result!Definition parseDefinition(ref Array!Token.Range tokens) @nogc

View File

@ -1,3 +1,3 @@
const a = 1, b = 2;
! + a b
! a + b
.

View File

@ -1,2 +1,2 @@
! + (+ 3 4) 1
! (3 + 4) + 1
.

View File

@ -1,2 +1,2 @@
! + 1 7
! 1 + 7
.

View File

@ -1,2 +1,2 @@
! + 1 (+ 3 4)
! 1 + (3 + 4)
.