-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionCallExpression.cs
More file actions
66 lines (52 loc) · 2.2 KB
/
Copy pathFunctionCallExpression.cs
File metadata and controls
66 lines (52 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
namespace SimpleCompiler
{
public class FunctionCallExpression : Expression
{
public string FunctionName { get; private set; }
public List<Expression> Args { get; private set; }
public override void Parse(TokensStack sTokens)
{
Args = new List<Expression>();
//function name
Token token = sTokens.Pop();
if( !(token is Identifier))
throw new SyntaxErrorException("Expected function name Identifier, received " + token, token);
FunctionName = ((Identifier)token).Name;
//check for '('
token = sTokens.Pop();
if (!(token is Parentheses) || ((Parentheses)token).Name != '(')
throw new SyntaxErrorException("Expected '(' for function args, received " + token, token);
//define args until closing ')' for args
token = sTokens.Peek();
while( !(token is Parentheses) || (((Parentheses)token).Name != ')'))
{
Expression expression = Expression.Create(sTokens);
expression.Parse(sTokens);
Args.Add(expression);
//expect additional arg if theres a comma
if (sTokens.Count > 0 && sTokens.Peek() is Separator)//,
{
token = sTokens.Pop();
if (((Separator)token).Name != ',') throw new SyntaxErrorException(@"Expected , (comma), received " + token, token);
}
token = sTokens.Peek();
}
//check for ')'
token = sTokens.Pop();
if (!(token is Parentheses) || ((Parentheses)token).Name != ')')
throw new SyntaxErrorException("Expected ')' for function args, received " + token, token);
}
public override string ToString()
{
string sFunction = FunctionName + "(";
for (int i = 0; i < Args.Count - 1; i++)
sFunction += Args[i] + ",";
if (Args.Count > 0)
sFunction += Args[Args.Count - 1];
sFunction += ")";
return sFunction;
}
}
}