Skip to content

Commit 3ffcb58

Browse files
committed
#200 - new ast node + parsing
1 parent a439865 commit 3ffcb58

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
2+
3+
namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Statements;
4+
5+
[AutoVisitable<IAbstractSyntaxTreeNode>]
6+
public partial class InputStatement : Statement
7+
{
8+
protected override IReadOnlyList<IAbstractSyntaxTreeNode> Children { get; }
9+
10+
public IdentifierReference Destination { get; }
11+
12+
public InputStatement(IdentifierReference destination)
13+
{
14+
Destination = destination;
15+
Destination.Parent = this;
16+
17+
Children = [Destination];
18+
}
19+
20+
protected override string NodeRepresentation() => "input";
21+
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/TopDownParser.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ private ScriptBody Script() =>
8383
private List<StatementListItem> StatementList()
8484
{
8585
var statementList = new List<StatementListItem>();
86-
while (CurrentIsDeclaration() || CurrentIsExpression() || CurrentIs("Output") ||
86+
while (CurrentIsDeclaration() || CurrentIsExpression() ||
87+
CurrentIs("Output") || CurrentIs("Input") ||
8788
CurrentIsKeyword("return") || CurrentIsKeyword("break") || CurrentIsKeyword("continue") ||
8889
CurrentIsKeyword("if") || CurrentIsKeyword("while"))
8990
{
@@ -115,6 +116,7 @@ private StatementListItem StatementListItem()
115116
/// BreakStatement
116117
/// ReturnStatement
117118
/// OutputStatement
119+
/// InputStatement
118120
/// </summary>
119121
private Statement Statement()
120122
{
@@ -149,6 +151,9 @@ private Statement Statement()
149151
if (CurrentIs("Output"))
150152
return OutputStatement();
151153

154+
if (CurrentIs("Input"))
155+
return InputStatement();
156+
152157
return null!;
153158
}
154159

@@ -228,6 +233,32 @@ private OutputStatement OutputStatement()
228233
return new OutputStatement(Expression());
229234
}
230235

236+
/// <summary>
237+
/// InputStatement -> '&lt;&lt;&lt;' (Ident | EnvVar)
238+
/// </summary>
239+
private InputStatement InputStatement()
240+
{
241+
var input = Expect("Input");
242+
if (CurrentIsOperator("$"))
243+
{
244+
var dollar = Expect("Operator");
245+
var envIdent = Expect("Ident");
246+
return new InputStatement(new EnvVarReference(envIdent.Value)
247+
{
248+
Segment = dollar.Segment + envIdent.Segment
249+
})
250+
{
251+
Segment = input.Segment + envIdent.Segment
252+
};
253+
}
254+
255+
var ident = Expect("Ident");
256+
return new InputStatement(new IdentifierReference(ident.Value) { Segment = ident.Segment })
257+
{
258+
Segment = input.Segment + ident.Segment
259+
};
260+
}
261+
231262
/// <summary>
232263
/// TypeDeclaration -> 'type' "Ident" = TypeValue
233264
/// </summary>

0 commit comments

Comments
 (0)