Skip to content

Commit d6b2d17

Browse files
More functions & various fixes
1 parent 27778dd commit d6b2d17

6 files changed

Lines changed: 116 additions & 17 deletions

File tree

fbl/Program.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ static void Main(string[] args)
5959

6060
Console.WriteLine("\n\n==-- RUN MAIN --==");
6161
interpreter.Run("main", new StringNode("world"));
62-
63-
/*
64-
File.WriteAllText(
65-
Path.GetFileNameWithoutExtension(args[0]) + "_gen.cpp",
66-
new CppGenerationVisitor().Visit(ast).ToString()
67-
);
68-
*/
69-
70-
Console.WriteLine("\n\nHALT");
7162
}
7263
}
7364
}

fbl/interpretation/modules/LanguageModule.cs

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@ void IModule.OnLoad(Interpreter interpreter)
4040
new FunctionNode(ToInt, context) { Parameter = new VariableNode("value") },
4141
context
4242
);
43+
interpreter.SetVariable(
44+
"number",
45+
new FunctionNode(ToNumber, context) { Parameter = new VariableNode("value") },
46+
context
47+
);
4348
interpreter.SetVariable(
4449
"string",
4550
new FunctionNode(ToString, context) { Parameter = new VariableNode("value") },
4651
context
4752
);
4853

54+
55+
interpreter.SetVariable(
56+
"abs",
57+
new FunctionNode(NumberAbsolute, context) { Parameter = new VariableNode("value") },
58+
context
59+
);
60+
61+
4962
interpreter.SetVariable(
5063
"+",
5164
new FunctionNode(Add, context) { Parameter = new VariableNode("left") },
@@ -56,6 +69,43 @@ void IModule.OnLoad(Interpreter interpreter)
5669
new FunctionNode(NumbersSub, context) { Parameter = new VariableNode("left") },
5770
context
5871
);
72+
interpreter.SetVariable(
73+
"*",
74+
new FunctionNode(NumbersMul, context) { Parameter = new VariableNode("left") },
75+
context
76+
);
77+
interpreter.SetVariable(
78+
"/",
79+
new FunctionNode(NumbersDiv, context) { Parameter = new VariableNode("left") },
80+
context
81+
);
82+
interpreter.SetVariable(
83+
"%",
84+
new FunctionNode(NumbersMod, context) { Parameter = new VariableNode("left") },
85+
context
86+
);
87+
88+
89+
interpreter.SetVariable("true", new NumberNode(1), context);
90+
interpreter.SetVariable("false", new NumberNode(0), context);
91+
interpreter.SetVariable(
92+
"not",
93+
new FunctionNode(
94+
(e) => new NumberNode(ToNumber(e).NumericValue == "0" ? 1 : 0),
95+
context
96+
)
97+
{ Parameter = new VariableNode("value") },
98+
context
99+
);
100+
interpreter.SetVariable(
101+
"if",
102+
new FunctionNode(
103+
IfExpression,
104+
context
105+
)
106+
{ Parameter = new VariableNode("condition") },
107+
context
108+
);
59109
}
60110

61111
ExpressionNode Import(ExpressionNode input)
@@ -64,6 +114,18 @@ ExpressionNode Import(ExpressionNode input)
64114
return new ExpressionNode();
65115
}
66116

117+
NumberNode NumberAbsolute(ExpressionNode input)
118+
=> new NumberNode(Math.Abs(decimal.Parse(ToNumber(input).NumericValue)));
119+
120+
ExpressionNode IfExpression(ExpressionNode condition)
121+
=> new FunctionNode((ExpressionNode onTrue)
122+
=> new FunctionNode(
123+
(ExpressionNode onFalse) => ToNumber(condition).NumericValue == "0" ? onFalse : onTrue,
124+
interpreter.GetGlobalContext())
125+
{ Parameter = new VariableNode("on_false") },
126+
interpreter.GetGlobalContext())
127+
{ Parameter = new VariableNode("on_true") };
128+
67129
ExpressionNode Set(ExpressionNode input)
68130
=> new FunctionNode((v) => interpreter.ChangeValue(input, v, input.Context), input.Context)
69131
{ Parameter = new VariableNode("right") };
@@ -89,6 +151,14 @@ NumberNode ToInt(ExpressionNode node)
89151
return new NumberNode("0", false);
90152
}
91153

154+
NumberNode ToNumber(ExpressionNode node)
155+
{
156+
if (decimal.TryParse(node.ToString(), out decimal value))
157+
return new NumberNode(value.ToString(), true);
158+
159+
return new NumberNode("0", false);
160+
}
161+
92162
StringNode ToString(ExpressionNode node) => new StringNode(node.ToString());
93163

94164

@@ -100,8 +170,8 @@ ExpressionNode Add(ExpressionNode left)
100170
if (left is NumberNode && right is NumberNode)
101171
{
102172
return new NumberNode(
103-
+ double.Parse(ToInt(left).NumericValue)
104-
+ double.Parse(ToInt(right).NumericValue));
173+
decimal.Parse(ToNumber(left).NumericValue)
174+
+ decimal.Parse(ToNumber(right).NumericValue));
105175
}
106176

107177
return new StringNode(left.ToString() + right.ToString());
@@ -114,9 +184,45 @@ ExpressionNode NumbersSub(ExpressionNode left)
114184
{
115185
return new FunctionNode(
116186
(right) => new NumberNode((
117-
+ int.Parse(ToInt(left).NumericValue)
118-
- int.Parse(ToInt(right).NumericValue)
119-
).ToString(), false)
187+
decimal.Parse(ToNumber(left).NumericValue)
188+
- decimal.Parse(ToNumber(right).NumericValue)
189+
).ToString(), true)
190+
, interpreter.GetGlobalContext()
191+
)
192+
{ Parameter = new VariableNode("right") };
193+
}
194+
195+
ExpressionNode NumbersMul(ExpressionNode left)
196+
{
197+
return new FunctionNode(
198+
(right) => new NumberNode((
199+
decimal.Parse(ToNumber(left).NumericValue)
200+
* decimal.Parse(ToNumber(right).NumericValue)
201+
).ToString(), true)
202+
, interpreter.GetGlobalContext()
203+
)
204+
{ Parameter = new VariableNode("right") };
205+
}
206+
207+
ExpressionNode NumbersDiv(ExpressionNode left)
208+
{
209+
return new FunctionNode(
210+
(right) => new NumberNode((
211+
decimal.Parse(ToNumber(left).NumericValue)
212+
/ decimal.Parse(ToNumber(right).NumericValue)
213+
).ToString(), true)
214+
, interpreter.GetGlobalContext()
215+
)
216+
{ Parameter = new VariableNode("right") };
217+
}
218+
219+
ExpressionNode NumbersMod(ExpressionNode left)
220+
{
221+
return new FunctionNode(
222+
(right) => new NumberNode((
223+
decimal.Parse(ToNumber(left).NumericValue)
224+
% decimal.Parse(ToNumber(right).NumericValue)
225+
).ToString(), true)
120226
, interpreter.GetGlobalContext()
121227
)
122228
{ Parameter = new VariableNode("right") };

fbl/parsing/nodes/FunctionNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public FunctionNode(Func<ExpressionNode, ExpressionNode> import, Context context
4646
/// <returns>Formatted string</returns>
4747
public override string ToString()
4848
{
49-
return $"(def [ {Parameter} ] {String.Join(" : ", Code ?? new StringNode("ø"))} )";
49+
return $"(def [ {Parameter?.Name ?? "ø"} ] {String.Join(" : ", Code ?? new StringNode("ø"))} )";
5050
}
5151

5252

fbl/parsing/nodes/data_holders/NumberNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public NumberNode(string value, bool isDecimal)
1515
Value = this;
1616
}
1717

18-
public NumberNode(double value)
18+
public NumberNode(decimal value)
1919
{
2020
NumericValue = value.ToString();
2121
IsDecimal = true;

fbl/tokenization/parsers/modules/CommentaryParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public static Token Parse(Tokenizer.State state)
1111
var begin = state.Index;
1212
state.Index += 1;
1313

14+
ParseInlineCommentary(state);
15+
1416
if (state.IsErrorOccured())
1517
state.Index = begin + 2;
1618

fbl/tokenization/parsers/modules/IdentifierParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static Token Parse(Tokenizer.State state)
2626

2727
private static bool IsMatching(char c, Tokenizer.State state)
2828
{
29-
const string possible = "!@#$%^&*_+~-=";
29+
const string possible = "!@#$%^&*/_+~-=";
3030
if (Char.IsLetterOrDigit(c) || possible.Contains(c))
3131
{
3232
if (c > 127)

0 commit comments

Comments
 (0)