-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBracketsBuilder.cs
More file actions
61 lines (52 loc) · 1.56 KB
/
BracketsBuilder.cs
File metadata and controls
61 lines (52 loc) · 1.56 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
using System;
using System.Text;
using System.Collections.Generic;
class BracketsBuilder : IBuilder<string> {
public string Number(string number) {
return number;
}
public string Variable(string name) {
return '$' + name;
}
public string Assignment(LexType op, string name, string value) {
return '(' + name + Scanner.OpNames[(int)op] + value + ')';
}
public string Field(string name) {
return name;
}
public string String(string str) {
throw new Exception("Strings are not (yet) allowed in brackets");
}
public string Function(string name, List<string> args) {
int limit = 0;
StringBuilder sb = new StringBuilder();
sb.Append(name);
sb.Append('(');
while (limit < args.Count) {
if (limit != 0) {
sb.Append(",");
}
sb.Append(args[limit]);
limit++;
}
sb.Append(')');
return sb.ToString();
}
public string Binary(LexType op, string left, string right) {
return '(' + left + Scanner.OpNames[(int)op] + right + ')';
}
public string Unary(LexType op, string expr) {
switch (op) {
case LexType.Minus:
case LexType.Plus:
return '(' + Scanner.OpNames[(int)op] + expr + ')';
case LexType.Exclamation:
return '(' + expr + '!' + ')';
default:
throw new Exception("Internal Error. Invalid unary operator.");
}
}
public string End(string expr) {
return expr;
}
}