while loops working
This commit is contained in:
@@ -8,11 +8,12 @@ public class Lexer
|
||||
{
|
||||
["func"] = Symbol.Func,
|
||||
["extern"] = Symbol.Extern,
|
||||
["return"] = Symbol.Return,
|
||||
["import"] = Symbol.Import,
|
||||
["let"] = Symbol.Let,
|
||||
["if"] = Symbol.If,
|
||||
["else"] = Symbol.Else,
|
||||
["while"] = Symbol.While,
|
||||
["return"] = Symbol.Return,
|
||||
};
|
||||
|
||||
private static readonly Dictionary<char[], Symbol> Chians = new()
|
||||
|
||||
@@ -15,6 +15,7 @@ public enum Symbol
|
||||
Let,
|
||||
If,
|
||||
Else,
|
||||
While,
|
||||
Semicolon,
|
||||
Colon,
|
||||
OpenParen,
|
||||
|
||||
@@ -163,25 +163,14 @@ public class Parser
|
||||
}
|
||||
case SymbolToken symbol:
|
||||
{
|
||||
switch (symbol.Symbol)
|
||||
return symbol.Symbol switch
|
||||
{
|
||||
case Symbol.Return:
|
||||
{
|
||||
return ParseReturn();
|
||||
}
|
||||
case Symbol.Let:
|
||||
{
|
||||
return ParseVariableAssignment();
|
||||
}
|
||||
case Symbol.If:
|
||||
{
|
||||
return ParseIf();
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new Exception($"Unexpected symbol {symbol.Symbol}");
|
||||
}
|
||||
}
|
||||
Symbol.Return => ParseReturn(),
|
||||
Symbol.Let => ParseVariableAssignment(),
|
||||
Symbol.If => ParseIf(),
|
||||
Symbol.While => ParseWhile(),
|
||||
_ => throw new Exception($"Unexpected symbol {symbol.Symbol}")
|
||||
};
|
||||
}
|
||||
default:
|
||||
{
|
||||
@@ -228,6 +217,13 @@ public class Parser
|
||||
return new IfNode(condition, body, elseStatement);
|
||||
}
|
||||
|
||||
private WhileNode ParseWhile()
|
||||
{
|
||||
var condition = ParseExpression();
|
||||
var body = ParseBlock();
|
||||
return new WhileNode(condition, body);
|
||||
}
|
||||
|
||||
private ExpressionNode ParseExpression(int precedence = 0)
|
||||
{
|
||||
var left = ParsePrimaryExpression();
|
||||
|
||||
7
Nub.Lang/Nub.Lang/Frontend/Parsing/WhileNode.cs
Normal file
7
Nub.Lang/Nub.Lang/Frontend/Parsing/WhileNode.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class WhileNode(ExpressionNode condition, BlockNode body) : StatementNode
|
||||
{
|
||||
public ExpressionNode Condition { get; } = condition;
|
||||
public BlockNode Body { get; } = body;
|
||||
}
|
||||
@@ -101,6 +101,9 @@ public class ExpressionTyper
|
||||
case VariableReassignmentNode variableReassignment:
|
||||
PopulateVariableReassignment(variableReassignment);
|
||||
break;
|
||||
case WhileNode whileStatement:
|
||||
PopulateWhileStatement(whileStatement);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(statement));
|
||||
}
|
||||
@@ -155,6 +158,12 @@ public class ExpressionTyper
|
||||
PopulateExpression(variableReassignment.Value);
|
||||
}
|
||||
|
||||
private void PopulateWhileStatement(WhileNode whileStatement)
|
||||
{
|
||||
PopulateExpression(whileStatement.Condition);
|
||||
PopulateBlock(whileStatement.Body);
|
||||
}
|
||||
|
||||
private void PopulateExpression(ExpressionNode expression)
|
||||
{
|
||||
switch (expression)
|
||||
|
||||
Reference in New Issue
Block a user