added tables and wrote the math parser

This commit is contained in:
Ryan 2017-08-24 01:29:57 -04:00
parent 42929b7f8c
commit 3a0bc4dfe0
4 changed files with 208 additions and 68 deletions

View File

@ -8,12 +8,11 @@
*/ */
using System; using System;
using parseManager; // IMPORTANT using parseManager; // IMPORTANT
using NCalc;
public class define : standardDefine // If you want the standard methods you must include this, Also this class cannot be static! public class define : standardDefine // If you want the standard methods you must include this, Also this class cannot be static!
{ {
public void testM(object arg1) public void testM(object arg1,object arg2)
{ {
Console.WriteLine(arg1); Console.WriteLine(arg1+"\t"+arg2);
} }
public void testM2(string arg1) public void testM2(string arg1)
{ {
@ -43,11 +42,7 @@ namespace parseManager
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
parseManager test = new parseManager("parsetest2.txt","define"); // define is where your methods will be held
parseManager test = new parseManager("parsetest2.txt"); // define is where your methods will be held
var env = test.GetENV();
env["test"]="TEST!";
env["test2"]=12345;
nextType next = test.Next(); // TODO implement the next method nextType next = test.Next(); // TODO implement the next method
string type; string type;
while(next.GetCMDType()!="EOF"){ while(next.GetCMDType()!="EOF"){

View File

@ -7,6 +7,8 @@ ENABLE leaking
"Test 1:" "Test 1:"
t=15 t=15
test=2*100 test=2*100
test2=[1,2,3,4,"HI"]
"test2 = $test2$"
"test = $test$" "test = $test$"
c=5 c=5
::HERE:: ::HERE::
@ -19,12 +21,13 @@ ENABLE leaking
"test = $test$" "test = $test$"
"stop = $stop$" "stop = $stop$"
"Test 2:" "Test 2:"
testM(2+2,"Test")
count=0 count=0
::loop:: ::loop::
if count>10 then GOTO("end")|GOTO("continue") if count>10 then GOTO("end")|GOTO("continue")
::continue:: ::continue::
"Count = $count$" "Count = $count$"
count=ADD(count,1) count=count+1
if count>10 then GOTO("end")|GOTO("loop") if count>10 then GOTO("end")|GOTO("loop")
::end:: ::end::
"Done!" "Done!"

View File

@ -12,7 +12,6 @@ using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Reflection; using System.Reflection;
using parseManager; using parseManager;
using NCalc;
namespace parseManager namespace parseManager
{ {
/// <summary> /// <summary>
@ -320,7 +319,8 @@ namespace parseManager
tempReturn.SetText("INVOKED METHOD: " + func); tempReturn.SetText("INVOKED METHOD: " + func);
} else if (type == "LINE") { } else if (type == "LINE") {
tempReturn.SetCMDType("line"); tempReturn.SetCMDType("line");
tempReturn.SetText(parseHeader((string)stuff[0])); var test=parseHeader((string)stuff[0]);
tempReturn.SetText(test.Substring(1,test.Length-2));
} else if (type == "FUNC_R") { } else if (type == "FUNC_R") {
var retargs = (string[])stuff[0]; var retargs = (string[])stuff[0];
var func = (string)stuff[1]; var func = (string)stuff[1];
@ -374,9 +374,23 @@ namespace parseManager
object val; object val;
double num; double num;
bool boo; bool boo;
double ex;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
if (isVar(v[i], out val)) { if (!v[i].StartsWith("["))
ex = evaluater.Evaluate(v[i]);
else
ex = double.NaN;
if (v[i].StartsWith("[")) {
var result = GLOBALS.Split(v[i].Substring(1, v[i].Length - 2)); // TODO make ENV
var res = ResolveVar(result);
var env = new ENV();
for (int g = 0; g < res.Length; g++) {
env[g] = res[g];
}
args[i] = env;
} else if (isVar(v[i], out val)) {
args[i] = val; args[i] = val;
debug("RETREVING SAVED VAL");
} else if (double.TryParse(v[i], out num)) { } else if (double.TryParse(v[i], out num)) {
args[i] = num; args[i] = num;
debug("NUMBER: " + num); debug("NUMBER: " + num);
@ -386,6 +400,8 @@ namespace parseManager
} else if (bool.TryParse(v[i], out boo)) { } else if (bool.TryParse(v[i], out boo)) {
args[i] = boo; args[i] = boo;
debug("BOOL: " + boo); debug("BOOL: " + boo);
} else if (!double.IsNaN(ex)) {
args[i] = ex;
} else { } else {
args[i] = null; args[i] = null;
} }
@ -438,14 +454,6 @@ namespace parseManager
var pureLine = Regex.Match(temp, "^\"(.+)\""); var pureLine = Regex.Match(temp, "^\"(.+)\"");
var FuncTest = Regex.Match(temp, @"([a-zA-Z0-9_]+)\s?\((.*)\)"); var FuncTest = Regex.Match(temp, @"([a-zA-Z0-9_]+)\s?\((.*)\)");
var assignment = Regex.Match(temp, "^([a-zA-Z0-9_,\\[\\]\"]+)=([a-zA-Z\\|&\\^\\+\\-\\*/%0-9_\",\\[\\]]+)"); var assignment = Regex.Match(temp, "^([a-zA-Z0-9_,\\[\\]\"]+)=([a-zA-Z\\|&\\^\\+\\-\\*/%0-9_\",\\[\\]]+)");
if (assignment.ToString() != "" && FuncTest.ToString()=="") { // TODO fix mess! or write own number calculator
var expression = new Expression(assignment.Groups[2].Value);
var extest = assignment.Groups[2].Value;
var res = Regex.Match(extest,@"[\+\-\*/%\^]");
if (!expression.HasErrors() && res.ToString()!="") {
temp=assignment.Groups[1].Value+"=CALC(\""+assignment.Groups[2]+"\")";
}
}
var FuncWReturn = Regex.Match(temp, "([\\[\\]\"a-zA-Z0-9_,]+)\\s?=\\s?([a-zA-Z0-9_]+)\\s?\\((.*)\\)"); var FuncWReturn = Regex.Match(temp, "([\\[\\]\"a-zA-Z0-9_,]+)\\s?=\\s?([a-zA-Z0-9_]+)\\s?\\((.*)\\)");
var FuncWOReturn = Regex.Match(temp, @"^([a-zA-Z0-9_]+)\s?\((.*)\)"); var FuncWOReturn = Regex.Match(temp, @"^([a-zA-Z0-9_]+)\s?\((.*)\)");
var label = Regex.Match(temp, "::(.*)::"); var label = Regex.Match(temp, "::(.*)::");
@ -459,10 +467,10 @@ namespace parseManager
var argselse = Regex.Match(tempelse, @"^([a-zA-Z0-9_]+)\s?\((.*)\)"); var argselse = Regex.Match(tempelse, @"^([a-zA-Z0-9_]+)\s?\((.*)\)");
string _funcif = (argsif.Groups[1]).ToString(); string _funcif = (argsif.Groups[1]).ToString();
var _argsif = (argsif.Groups[2]).ToString(); var _argsif = (argsif.Groups[2]).ToString();
string[] _resultif = Regex.Split(_argsif, ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)"); string[] _resultif = Regex.Split(_argsif, ",(?=(?:[^\"'\\[\\]]*[\"'\\[\\]][^\"'\\[\\]]*[\"'\\[\\]])*[^\"'\\[\\]]*$)");
string _funcelse = (argselse.Groups[1]).ToString(); string _funcelse = (argselse.Groups[1]).ToString();
var _argselse = (argselse.Groups[2]).ToString(); var _argselse = (argselse.Groups[2]).ToString();
string[] _resultelse = Regex.Split(_argselse, ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)"); string[] _resultelse = Regex.Split(_argselse, ",(?=(?:[^\"'\\[\\]]*[\"'\\[\\]][^\"'\\[\\]]*[\"'\\[\\]])*[^\"'\\[\\]]*$)");
var mm = Regex.Matches(condition, "(.+?)([and ]+?[or ]+)"); var mm = Regex.Matches(condition, "(.+?)([and ]+?[or ]+)");
var conds = new string[(mm.Count + 1) * 3]; var conds = new string[(mm.Count + 1) * 3];
var andors = new string[mm.Count]; var andors = new string[mm.Count];
@ -505,7 +513,7 @@ namespace parseManager
var func = (FuncWReturn.Groups[2]).ToString(); var func = (FuncWReturn.Groups[2]).ToString();
var args = (FuncWReturn.Groups[3]).ToString(); var args = (FuncWReturn.Groups[3]).ToString();
var retargs = var1.Split(','); var retargs = var1.Split(',');
var result = Regex.Split(args, ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)"); var result = Regex.Split(args, ",(?=(?:[^\"'\\[\\]]*[\"'\\[\\]][^\"'\\[\\]]*[\"'\\[\\]])*[^\"'\\[\\]]*$)");
_compiledlines.Add(new CMD("FUNC_R", new object[] { _compiledlines.Add(new CMD("FUNC_R", new object[] {
retargs, retargs,
func, func,
@ -514,13 +522,15 @@ namespace parseManager
} else if (FuncWOReturn.ToString() != "") { } else if (FuncWOReturn.ToString() != "") {
var func = (FuncWOReturn.Groups[1]).ToString(); var func = (FuncWOReturn.Groups[1]).ToString();
var args = (FuncWOReturn.Groups[2]).ToString(); var args = (FuncWOReturn.Groups[2]).ToString();
var result = Regex.Split(args, ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)"); var result = Regex.Split(args, ",(?=(?:[^\"'\\[\\]]*[\"'\\[\\]][^\"'\\[\\]]*[\"'\\[\\]])*[^\"'\\[\\]]*$)");
_compiledlines.Add(new CMD("FUNC", new object[]{ func, result })); _compiledlines.Add(new CMD("FUNC", new object[]{ func, result }));
} else if (pureLine.ToString() != "") { } else if (pureLine.ToString() != "") {
_compiledlines.Add(new CMD("LINE", new object[]{ pureLine.ToString() })); _compiledlines.Add(new CMD("LINE", new object[]{ pureLine.ToString() }));
} else if (assignment.ToString() != "") { } else if (assignment.ToString() != "") {
var vars = Regex.Split(assignment.Groups[1].ToString(), ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)"); var vars = Regex.Split(assignment.Groups[1].ToString(), ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)");
var vals = Regex.Split(assignment.Groups[2].ToString(), ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)"); var tabTest = assignment.Groups[2].ToString();
string[] vals = GLOBALS.Split(tabTest);
//vals = Regex.Split(assignment.Groups[2].ToString(), ",(?=(?:[^\"'\\[\\]]*[\"'\\[\\]][^\"'\\[\\]]*[\"'\\[\\]])*[^\"'\\[\\]]*$)");
_compiledlines.Add(new CMD("ASSIGN", new object[]{ vars, vals })); _compiledlines.Add(new CMD("ASSIGN", new object[]{ vars, vals }));
} else { } else {
_compiledlines.Add(new CMD("UNKNOWN", new object[]{ })); _compiledlines.Add(new CMD("UNKNOWN", new object[]{ }));
@ -613,14 +623,117 @@ namespace parseManager
return _type; return _type;
} }
} }
public static class evaluater
{
static double helper(ENV env, parseManager PM, string o, object _v, object _r)
{
double v = 0;
double r = 0;
if (_v.GetType().ToString() == "System.String") {
object temp;
if (env.TryGetValue((string)_v, out temp)) {
v = (double)temp;
} else if (double.TryParse((string)_v, out v)) {
// We good!
} else {
PM.PushError("Attempt to do arithmetic on a null value: " + _v);
}
} else {
v = (double)_v;
}
if (_r.GetType().ToString() == "System.String") {
object temp;
if (env.TryGetValue((string)_r, out temp)) {
r = (double)temp;
} else if (double.TryParse((string)_r, out r)) {
// We good!
} else {
PM.PushError("Attempt to do arithmetic on a null value: " + _r);
}
} else {
v = (double)_v;
}
// Constructs?
if (o == "+") {
return r + v;
} else if (o == "-") {
return r - v;
} else if (o == "/") {
return r / v;
} else if (o == "*") {
return r * v;
} else if (o == "^") {
return Math.Pow(r, v);
} else if (o == "%") {
return r % v;
}
return 0;
}
public static double Evaluate(string cmd)
{
return Evaluate(cmd, 0);
}
public static double Evaluate(string cmd, double v)
{
double test;
if (double.TryParse(cmd, out test)) {
return test;
}
var loop = false;
if (cmd.Substring(0, 1) == "-") {
cmd = "0" + cmd;
}
var PM = GLOBALS.GetPM();
var env = PM.GetENV();
var count = 0;
var para = Regex.Match(cmd, @"(\(.+\))");
if (para.ToString() != "") {
return Evaluate(para.Groups[1].Value.Substring(1, para.Groups[1].Value.Length - 2));
}
foreach (Match m in Regex.Matches(cmd,@"(.*)([/%\+\^\-\*])(.*)")) {
loop = true;
count++;
var l = m.Groups[1].Value;
var o = m.Groups[2].Value;
var r = m.Groups[3].Value;
if (Regex.Match(l, @"([/%\+\^\-\*])").ToString() != "") {
v = Evaluate(l, v);
v = helper(env, PM, o, r, v);
} else if (count == 1) {
v = helper(env, PM, o, r, l);
}
}
if (!loop) {
object t;
if (env.TryGetValue(cmd, out t)) {
return (double)t;
} else {
t = double.NaN; // It couldn't be done :'(
}
}
return v; // TODO grab current ENV and does the calculation
}
}
public class ENV public class ENV
{ {
ENV _Parent; ENV _Parent;
Dictionary<string, object> _vars = new Dictionary<string, object>(); Dictionary<string, object> _vars = new Dictionary<string, object>();
Dictionary<int, object> _varsI = new Dictionary<int, object>();
void SetParent(ENV other) void SetParent(ENV other)
{ {
_Parent = other; _Parent = other;
} }
public override string ToString()
{
string str = "";
foreach (KeyValuePair<string, object> entry in _vars) {
str+=entry.Key+" = "+entry.Value+",";
}
foreach (KeyValuePair<int, object> entry in _varsI) {
str+="["+entry.Key+"] = "+entry.Value+", ";
}
return str;
}
public bool TryGetValue(string ind, out object obj) public bool TryGetValue(string ind, out object obj)
{ {
if (this[ind] != null) { if (this[ind] != null) {
@ -630,6 +743,15 @@ namespace parseManager
obj = null; obj = null;
return false; return false;
} }
public bool TryGetValue(int ind, out object obj)
{
if (this[ind] != null) {
obj = this[ind];
return true;
}
obj = null;
return false;
}
public object this[string ind] { public object this[string ind] {
get { get {
object obj; object obj;
@ -638,32 +760,6 @@ namespace parseManager
} }
if (_Parent != null) { if (_Parent != null) {
return _Parent[ind]; return _Parent[ind];
} else {
return null;
}
}
set {
_vars[ind] = value;
}
}
}
public class PList
{
readonly Dictionary<int, object> _vars = new Dictionary<int, object>();
public bool TryGetValue(int ind, out object obj)
{
if (this[ind] != null) {
obj = this[ind];
return true;
}
obj = null;
return false;
}
public object this[int ind] {
get {
object obj;
if (_vars.TryGetValue(ind, out obj)) {
return obj;
} }
return null; return null;
} }
@ -671,6 +767,21 @@ namespace parseManager
_vars[ind] = value; _vars[ind] = value;
} }
} }
public object this[int ind] {
get {
object obj;
if (_varsI.TryGetValue(ind, out obj)) {
return obj;
}
if (_Parent != null) {
return _Parent[ind];
}
return null;
}
set {
_varsI[ind] = value;
}
}
} }
public class nextType public class nextType
{ {
@ -772,6 +883,53 @@ namespace parseManager
{ {
return _numvars.ToArray(); return _numvars.ToArray();
} }
public static string[] Split(string split)
{
var res = new List<string>();
var state = 0;
var c = ".";
var elem = "";
for (int gg = 0; gg < split.Length; gg++) {
c = split.Substring(gg, 1);
if (state == 3 || state == 0) {
if (state == 3 && c == " ") {
state = 0;
} else {
state = 0;
if (c == "\"" || c == "'") {
state = 1;
elem += "\"";
} else if (c == "[") {
state = 1;
elem += "[";
} else if (c == ",") {
res.Add(elem);
elem = "";
state = 3;
} else {
elem += c;
}
}
} else if (state == 1) {
if (c == "\"" || c == "'") {
state = 0;
elem += "\"";
} else if (c == "]") {
state = 0;
elem += "]";
} else if (c == "\\") {
state = 2;
} else {
elem += c;
}
} else if (state == 2) {
elem += c;
state = 1;
}
}
res.Add(elem);
return res.ToArray();
}
} }
} }
public class standardDefine public class standardDefine
@ -841,19 +999,6 @@ public class standardDefine
} }
public double CALC(string ex) public double CALC(string ex)
{ {
try { return evaluater.Evaluate(ex);
Expression e = new Expression(ex);
var vars = GLOBALS.GetVars();
var PM = GLOBALS.GetPM();
var env = PM.GetENV();
for (int i = 0; i < vars.Length; i++) {
if (env[vars[i]].GetType().ToString().Contains("Double"))
e.Parameters[vars[i]] = (double)env[vars[i]];
}
return double.Parse(e.Evaluate().ToString());
} catch {
GLOBALS.GetPM().PushError("Invalid Expression: "+ex);
return double.NaN;
}
} }
} }

View File

@ -34,9 +34,6 @@
<Reference Include="Microsoft.CSharp"> <Reference Include="Microsoft.CSharp">
<RequiredTargetFramework>4.0</RequiredTargetFramework> <RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference> </Reference>
<Reference Include="NCalc">
<HintPath>bin\Debug\NCalc.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>