Skip to content

Commit dd14d40

Browse files
committed
added fully working expression parser, finally.
1 parent b4faf21 commit dd14d40

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

Core/Logic/Operator.cs

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public Operator(string symbol, Func<Value, Value, Value> calculate = null) {
3131
this.execute = calculate;
3232
}
3333

34+
public int compare(Operator op)
35+
{
36+
return importance - op.importance;
37+
}
38+
39+
public int importance => (symbol == CALCULATE_ADD || symbol == CALCULATE_SUBTRACT ? 1 : (symbol == CALCULATE_MULTIPLY || symbol == CALCULATE_DIVIDE ? 2 : 0));
40+
3441
public static Value getRealValue(Value v) {
3542
if (v is Reference) return (v as Reference).getTrueValue();
3643
else return v;

Core/Logic/Segment.cs

+26-17
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public virtual Value execute(Qontext context) {
3838
reset();
3939
returning = false;
4040
head = new Node();
41-
Node end = build(context);
42-
Log.spam("executing node:\n" + end);
41+
Node tail = build(context, new Node());
42+
Log.spam("executing node:\n" + tail);
4343
Value r = Value.Null;
44-
if (end.empty() && !head.empty()) {
44+
if (tail.empty() && !head.empty()) {
4545
r = head.execute();
4646
} else {
47-
r = end.execute();
47+
r = tail.execute();
4848
if (!head.empty()) {
4949
head.right = r;
5050
head.execute();
@@ -90,12 +90,28 @@ protected virtual Node build(Qontext context, Node node = null) {
9090
}
9191
}
9292
if (node.ready()) {
93-
Node next = new Node(node, null, op);
94-
node = build(context, next);
93+
Log.spam("comparing operators to chain nodes");
94+
if (op.compare(node.op) > 0)
95+
node.right = build(context, new Node(node.right, null, op));
96+
else
97+
node = build(context, new Node(node, null, op));
98+
} else if (head.op == null && (
99+
op.symbol == Operator.ASSIGN_VALUE ||
100+
op.symbol == Operator.ASSIGN_REFERENCE)) {
101+
head.op = op;
102+
} else if (node.left == null || (node.op != null && node.right == null)) {
103+
if (op.symbol == Operator.CALCULATE_ADD) continue;
104+
else if (op.symbol == Operator.CALCULATE_SUBTRACT) {
105+
var neg = new Segment(new Token[] {
106+
Token.create(ValueType.Number, "-1"),
107+
Token.create(ValueType.Operator, Operator.CALCULATE_MULTIPLY),
108+
digest() });
109+
Log.spam("created negating segment: " + neg.ToString());
110+
if (node.left == null) node.left = neg.execute(context);
111+
else node.right = neg.execute(context);
112+
} else throw new ParseException("unexpected operator " + op.symbol + " at start of expression", t);
95113
} else if (node.left != null) {
96114
node.op = op;
97-
} else if (head.op == null) {
98-
head.op = op;
99115
} else throw new ParseException("sorry, manipulation operators (operators without left-hand value) are not yet implemented. :/", t);
100116
} else if (t.check(ValueType.Struqture, "(")) {
101117
if (node.ready()) throw new ParseException("can not open new segment without prior operator.", t);
@@ -176,15 +192,8 @@ public bool put(object value) {
176192
}
177193

178194
public override string ToString() {
179-
if (empty()) return "{ }";
180-
string r = "{\n";
181-
r += " " + (right is Node ? left?.ToString() : right?.ToString()) + " " + op?.symbol;
182-
if (ready()) {
183-
string[] s = (right is Node ? right?.ToString() : left?.ToString()).Split(new char[] { '\n' });
184-
r += " " + s[0] + " ";
185-
for (int i = 1; i < s.Length; i++) r += "\n " + s[i];
186-
}
187-
return r += "\n}";
195+
if (empty()) return "{ void }";
196+
return "{ " + (left ?? "null") + " " + (op == null ? "NaO" : op.symbol) + " " + (right ?? "null") + " }";
188197
}
189198
}
190199
}

Core/Runtime.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ static void cli() {
462462
content += "\n";
463463
Log.write(" ", ConsoleColor.White, "");
464464
}
465-
} while (c.Key != ConsoleKey.Escape);
465+
} while (true); //c.Key != ConsoleKey.Escape);
466466
if (content.StartsWith("#run")) {
467467
content = File.ReadAllText(content.Substring(5) + (content.EndsWith(".sq") ? "" : ".sq"));
468468
Log.info("executing:\n" + content);

Core/Types/Obqect.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ namespace Qrakhen.Sqript
44
{
55
internal class Obqect : Qontext
66
{
7-
public Obqect(Qontext parent, Dictionary<string, Reference> value, bool extendable = true) : base(parent, ValueType.Obqect, value, extendable) {
8-
}
7+
public Obqect(Qontext parent, Dictionary<string, Reference> value, bool extendable = true)
8+
: base(parent, ValueType.Obqect, value, extendable) { }
99

10-
public Obqect(Qontext parent, bool extendable = true) : this(parent, new Dictionary<string, Reference>(), extendable) {}
10+
public Obqect(Qontext parent, bool extendable = true)
11+
: this(parent, new Dictionary<string, Reference>(), extendable) {}
1112
}
1213
}

Sqript.sln

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.26430.16
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SqriptRuntime", "SqriptRuntime.csproj", "{F0E0B230-B782-4137-A67D-6F8D38775BB1}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sqript.Lib", "..\Sqript.Lib\Sqript.Lib.csproj", "{02C5AD9B-4A20-4D93-B005-6CCB4F816A22}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sqript.Libs", "..\Sqript.Libs\Sqript.Libs.csproj", "{EECFB58E-E041-42A6-957A-B19988DE299D}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -20,14 +20,17 @@ Global
2020
{F0E0B230-B782-4137-A67D-6F8D38775BB1}.Release|Any CPU.Build.0 = Release|Any CPU
2121
{F0E0B230-B782-4137-A67D-6F8D38775BB1}.win32_exe|Any CPU.ActiveCfg = Debug|Any CPU
2222
{F0E0B230-B782-4137-A67D-6F8D38775BB1}.win32_exe|Any CPU.Build.0 = Debug|Any CPU
23-
{02C5AD9B-4A20-4D93-B005-6CCB4F816A22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24-
{02C5AD9B-4A20-4D93-B005-6CCB4F816A22}.Debug|Any CPU.Build.0 = Debug|Any CPU
25-
{02C5AD9B-4A20-4D93-B005-6CCB4F816A22}.Release|Any CPU.ActiveCfg = Release|Any CPU
26-
{02C5AD9B-4A20-4D93-B005-6CCB4F816A22}.Release|Any CPU.Build.0 = Release|Any CPU
27-
{02C5AD9B-4A20-4D93-B005-6CCB4F816A22}.win32_exe|Any CPU.ActiveCfg = Debug|Any CPU
28-
{02C5AD9B-4A20-4D93-B005-6CCB4F816A22}.win32_exe|Any CPU.Build.0 = Debug|Any CPU
23+
{EECFB58E-E041-42A6-957A-B19988DE299D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{EECFB58E-E041-42A6-957A-B19988DE299D}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{EECFB58E-E041-42A6-957A-B19988DE299D}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{EECFB58E-E041-42A6-957A-B19988DE299D}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{EECFB58E-E041-42A6-957A-B19988DE299D}.win32_exe|Any CPU.ActiveCfg = Debug|Any CPU
28+
{EECFB58E-E041-42A6-957A-B19988DE299D}.win32_exe|Any CPU.Build.0 = Debug|Any CPU
2929
EndGlobalSection
3030
GlobalSection(SolutionProperties) = preSolution
3131
HideSolutionNode = FALSE
3232
EndGlobalSection
33+
GlobalSection(ExtensibilityGlobals) = postSolution
34+
SolutionGuid = {DD17708F-B5B8-49BA-8EC8-1D7FD4EC05D3}
35+
EndGlobalSection
3336
EndGlobal

SqriptRuntime.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
66
<AssemblyName>Qrakhen.Sqript</AssemblyName>
77
<RootNamespace>Qrakhen.Sqript</RootNamespace>
88
<Version>0.2.0</Version>

0 commit comments

Comments
 (0)