Started to implement expressions
This commit is contained in:
parent
5a990e4313
commit
74324b86c3
@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
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)
|
||||||
@ -42,7 +43,8 @@ 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();
|
var env = test.GetENV();
|
||||||
env["test"]="TEST!";
|
env["test"]="TEST!";
|
||||||
env["test2"]=12345;
|
env["test2"]=12345;
|
||||||
@ -56,7 +58,6 @@ namespace parseManager
|
|||||||
}
|
}
|
||||||
next = test.Next();
|
next = test.Next();
|
||||||
}
|
}
|
||||||
//var temp=test.InvokeR("TEST",new object[]{});
|
|
||||||
Console.Write("Press any key to continue . . . ");
|
Console.Write("Press any key to continue . . . ");
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,9 @@ ENABLE leaking
|
|||||||
}
|
}
|
||||||
[START]{
|
[START]{
|
||||||
"Test 1:"
|
"Test 1:"
|
||||||
num=ADD(5,5)
|
t=15
|
||||||
|
test=2+t
|
||||||
|
"test = $test$"
|
||||||
"num = $num$"
|
"num = $num$"
|
||||||
c=5
|
c=5
|
||||||
::HERE::
|
::HERE::
|
||||||
|
|||||||
@ -12,6 +12,7 @@ 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>
|
||||||
@ -19,8 +20,8 @@ namespace parseManager
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class parseManager
|
public class parseManager
|
||||||
{
|
{
|
||||||
|
standardDefine _invoke = new standardDefine();
|
||||||
string _filepath;
|
string _filepath;
|
||||||
bool _hasDefine;
|
|
||||||
bool _active = true;
|
bool _active = true;
|
||||||
string _define;
|
string _define;
|
||||||
string _entry = "START";
|
string _entry = "START";
|
||||||
@ -34,6 +35,27 @@ namespace parseManager
|
|||||||
Dictionary<string, bool> _flags = new Dictionary<string, bool>();
|
Dictionary<string, bool> _flags = new Dictionary<string, bool>();
|
||||||
Dictionary<string, chunk> _chunks = new Dictionary<string, chunk>();
|
Dictionary<string, chunk> _chunks = new Dictionary<string, chunk>();
|
||||||
Dictionary<string, string> _methods = new Dictionary<string, string>();
|
Dictionary<string, string> _methods = new Dictionary<string, string>();
|
||||||
|
public parseManager(string filepath)
|
||||||
|
{
|
||||||
|
InitFlags();
|
||||||
|
_filepath = filepath;
|
||||||
|
_defineType = Type.GetType("standardDefine");
|
||||||
|
ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes);
|
||||||
|
_defineClassObject = defineConstructor.Invoke(new object[]{ });
|
||||||
|
_defualtENV = _mainENV;
|
||||||
|
Parse();
|
||||||
|
}
|
||||||
|
public parseManager(string filepath, string define)
|
||||||
|
{
|
||||||
|
InitFlags();
|
||||||
|
_define = define;
|
||||||
|
_filepath = filepath;
|
||||||
|
_defineType = Type.GetType(define);
|
||||||
|
ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes);
|
||||||
|
_defineClassObject = defineConstructor.Invoke(new object[]{ });
|
||||||
|
_defualtENV = _mainENV;
|
||||||
|
Parse();
|
||||||
|
}
|
||||||
void InitFlags()
|
void InitFlags()
|
||||||
{
|
{
|
||||||
_flags.Add("leaking", false);
|
_flags.Add("leaking", false);
|
||||||
@ -41,7 +63,8 @@ namespace parseManager
|
|||||||
_flags.Add("debugging", false);
|
_flags.Add("debugging", false);
|
||||||
_flags.Add("topdown", true);
|
_flags.Add("topdown", true);
|
||||||
}
|
}
|
||||||
public bool GetFlag(string flag){
|
public bool GetFlag(string flag)
|
||||||
|
{
|
||||||
bool f;
|
bool f;
|
||||||
if (_flags.TryGetValue(flag, out f)) {
|
if (_flags.TryGetValue(flag, out f)) {
|
||||||
return f;
|
return f;
|
||||||
@ -104,12 +127,12 @@ namespace parseManager
|
|||||||
{
|
{
|
||||||
_Parse(code);
|
_Parse(code);
|
||||||
}
|
}
|
||||||
public chunk[] GetChunks(){
|
public chunk[] GetChunks()
|
||||||
|
{
|
||||||
var chunks = _chunks.Values;
|
var chunks = _chunks.Values;
|
||||||
var temp = new chunk[_chunks.Count];
|
var temp = new chunk[_chunks.Count];
|
||||||
var i = 0;
|
var i = 0;
|
||||||
foreach(var item in _chunks)
|
foreach (var item in _chunks) {
|
||||||
{
|
|
||||||
temp[i] = item.Value;
|
temp[i] = item.Value;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -123,40 +146,26 @@ namespace parseManager
|
|||||||
{
|
{
|
||||||
_active = false;
|
_active = false;
|
||||||
}
|
}
|
||||||
public parseManager(string filepath)
|
|
||||||
{
|
|
||||||
InitFlags();
|
|
||||||
_filepath = filepath;
|
|
||||||
_hasDefine = false;
|
|
||||||
_defualtENV = _mainENV;
|
|
||||||
Parse();
|
|
||||||
}
|
|
||||||
public parseManager(string filepath, string define)
|
|
||||||
{
|
|
||||||
InitFlags();
|
|
||||||
_define = define;
|
|
||||||
_hasDefine = true;
|
|
||||||
_filepath = filepath;
|
|
||||||
_defineType = Type.GetType(define);
|
|
||||||
ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes);
|
|
||||||
_defineClassObject = defineConstructor.Invoke(new object[]{ });
|
|
||||||
_defualtENV = _mainENV;
|
|
||||||
Parse();
|
|
||||||
}
|
|
||||||
public object InvokeR(string method, object[] args)
|
public object InvokeR(string method, object[] args)
|
||||||
{ // TODO collect the returned arguments if any
|
{
|
||||||
if (!_hasDefine)
|
//try{
|
||||||
return null;
|
|
||||||
_defineMethod = _defineType.GetMethod(method);
|
_defineMethod = _defineType.GetMethod(method);
|
||||||
return _defineMethod.Invoke(_defineClassObject, args);
|
return _defineMethod.Invoke(_defineClassObject, args);
|
||||||
|
// } catch {
|
||||||
|
// PushError("Invalid method: "+method);
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
public int InvokeNR(string method, object[] args)
|
public int InvokeNR(string method, object[] args)
|
||||||
{ // Simple Invoking!
|
{
|
||||||
if (!_hasDefine)
|
try {
|
||||||
return -1;
|
|
||||||
_defineMethod = _defineType.GetMethod(method);
|
_defineMethod = _defineType.GetMethod(method);
|
||||||
_defineMethod.Invoke(_defineClassObject, args);
|
_defineMethod.Invoke(_defineClassObject, args);
|
||||||
return 0;
|
return 0;
|
||||||
|
} catch {
|
||||||
|
PushError("Invalid method: " + method);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
public void SetBlock(string BLOCK)
|
public void SetBlock(string BLOCK)
|
||||||
{
|
{
|
||||||
@ -239,8 +248,6 @@ namespace parseManager
|
|||||||
var objs = new object[conds.Length]; // contain the actual values of what is in the env
|
var objs = new object[conds.Length]; // contain the actual values of what is in the env
|
||||||
var truths = new bool[conds.Length / 3];
|
var truths = new bool[conds.Length / 3];
|
||||||
var c = 0;
|
var c = 0;
|
||||||
//Console.WriteLine(string.Join(",",conds));
|
|
||||||
//Console.WriteLine(string.Join(",",andors));
|
|
||||||
for (int i = 0; i < conds.Length; i += 3) {
|
for (int i = 0; i < conds.Length; i += 3) {
|
||||||
var condA = (object)ResolveVar(new []{ conds[i] })[0];
|
var condA = (object)ResolveVar(new []{ conds[i] })[0];
|
||||||
var e = conds[i + 1];
|
var e = conds[i + 1];
|
||||||
@ -326,15 +333,17 @@ namespace parseManager
|
|||||||
}
|
}
|
||||||
var env = GetENV();
|
var env = GetENV();
|
||||||
env[retargs[0]] = data;
|
env[retargs[0]] = data;
|
||||||
|
GLOBALS.Add_Var(retargs[0]);
|
||||||
tempReturn.SetCMDType("method");
|
tempReturn.SetCMDType("method");
|
||||||
tempReturn.SetText("INVOKED METHOD: " + func);
|
tempReturn.SetText("INVOKED METHOD: " + func);
|
||||||
} else if (type == "ASSIGN") {
|
} else if (type == "ASSIGN") { // TODO add lists/dictonaries support
|
||||||
var vars = (string[])stuff[0];
|
var vars = (string[])stuff[0];
|
||||||
var vals = (string[])stuff[1];
|
var vals = (string[])stuff[1];
|
||||||
var env = GetENV();
|
var env = GetENV();
|
||||||
var types = ResolveVar(vals);
|
var types = ResolveVar(vals);
|
||||||
for (int i = 0; i < types.Length; i++) {
|
for (int i = 0; i < types.Length; i++) {
|
||||||
env[vars[i]] = types[i];
|
env[vars[i]] = types[i];
|
||||||
|
GLOBALS.Add_Var(vars[i]);
|
||||||
}
|
}
|
||||||
tempReturn.SetCMDType("assignment");
|
tempReturn.SetCMDType("assignment");
|
||||||
tempReturn.SetText(_currentChunk.GetLine());
|
tempReturn.SetText(_currentChunk.GetLine());
|
||||||
@ -426,12 +435,19 @@ namespace parseManager
|
|||||||
string temp;
|
string temp;
|
||||||
for (int i = 0; i < _lines.Length - 1; i++) {
|
for (int i = 0; i < _lines.Length - 1; i++) {
|
||||||
temp = _lines[i];
|
temp = _lines[i];
|
||||||
|
var pureLine = Regex.Match(temp, "^\"(.+)\"");
|
||||||
|
var assignment = Regex.Match(temp, "^([a-zA-Z0-9_,\\[\\]\"]+)=([a-zA-Z\\|&\\^\\+\\-\\*/%0-9_\",\\[\\]]+)");
|
||||||
|
if (assignment.ToString() != "") { // TODO fix mess! or write own number calculator
|
||||||
|
var expression = new Expression(assignment.Groups[2].Value);
|
||||||
|
if (!expression.HasErrors()) {
|
||||||
|
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 pureLine = Regex.Match(temp, "^\"(.+)\"");
|
|
||||||
var assignment = Regex.Match(temp, "^([a-zA-Z0-9_,\\[\\]\"]+)=([a-zA-Z0-9_\",\\[\\]]+)");
|
|
||||||
var label = Regex.Match(temp, "::(.*)::");
|
var label = Regex.Match(temp, "::(.*)::");
|
||||||
var logic = Regex.Match(temp, @"if\s*(.+)\s*then\s*(.+?\))\s*\|\s*(.+?\))");
|
var logic = Regex.Match(temp, @"if\s*(.+)\s*then\s*(.+?\))\s*\|\s*(.+?\))");
|
||||||
|
|
||||||
if (logic.ToString() != "") {
|
if (logic.ToString() != "") {
|
||||||
var condition = logic.Groups[1].ToString();
|
var condition = logic.Groups[1].ToString();
|
||||||
var tempif = logic.Groups[2].ToString();
|
var tempif = logic.Groups[2].ToString();
|
||||||
@ -473,7 +489,14 @@ namespace parseManager
|
|||||||
conds[p++] = s1a2;
|
conds[p++] = s1a2;
|
||||||
conds[p++] = s1b2;
|
conds[p++] = s1b2;
|
||||||
conds[p++] = s1c2;
|
conds[p++] = s1c2;
|
||||||
_compiledlines.Add(new CMD("LOGIC", new object[]{conds,andors,_funcif,_resultif,_funcelse,_resultelse}));
|
_compiledlines.Add(new CMD("LOGIC", new object[] {
|
||||||
|
conds,
|
||||||
|
andors,
|
||||||
|
_funcif,
|
||||||
|
_resultif,
|
||||||
|
_funcelse,
|
||||||
|
_resultelse
|
||||||
|
}));
|
||||||
} else if (label.ToString() != "") {
|
} else if (label.ToString() != "") {
|
||||||
_labels[label.Groups[1].ToString()] = i;
|
_labels[label.Groups[1].ToString()] = i;
|
||||||
_compiledlines.Add(new CMD("LABEL", new object[]{ }));
|
_compiledlines.Add(new CMD("LABEL", new object[]{ }));
|
||||||
@ -483,7 +506,11 @@ namespace parseManager
|
|||||||
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[] { retargs, func, result }));
|
_compiledlines.Add(new CMD("FUNC_R", new object[] {
|
||||||
|
retargs,
|
||||||
|
func,
|
||||||
|
result
|
||||||
|
}));
|
||||||
} 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();
|
||||||
@ -512,7 +539,8 @@ namespace parseManager
|
|||||||
_type = "CODEBLOCK";
|
_type = "CODEBLOCK";
|
||||||
_clean(cont);
|
_clean(cont);
|
||||||
}
|
}
|
||||||
public string GetName(){
|
public string GetName()
|
||||||
|
{
|
||||||
return _BLOCK;
|
return _BLOCK;
|
||||||
}
|
}
|
||||||
public int GetLabel(string lab)
|
public int GetLabel(string lab)
|
||||||
@ -529,7 +557,8 @@ namespace parseManager
|
|||||||
pos = -1;
|
pos = -1;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public void RemoveNextChunk(){
|
public void RemoveNextChunk()
|
||||||
|
{
|
||||||
_next = null;
|
_next = null;
|
||||||
}
|
}
|
||||||
public void SetNextChunk(chunk next)
|
public void SetNextChunk(chunk next)
|
||||||
@ -705,23 +734,43 @@ namespace parseManager
|
|||||||
*/
|
*/
|
||||||
static class GLOBALS
|
static class GLOBALS
|
||||||
{
|
{
|
||||||
static parseManager current;
|
static standardDefine _define = new standardDefine();
|
||||||
static readonly ENV env = new ENV();
|
static parseManager _current;
|
||||||
|
static readonly ENV _env = new ENV();
|
||||||
|
static List<string> _numvars = new List<string>();
|
||||||
public static object GetData(string ind)
|
public static object GetData(string ind)
|
||||||
{
|
{
|
||||||
return env[ind];
|
return _env[ind];
|
||||||
|
}
|
||||||
|
public static standardDefine GetDefine()
|
||||||
|
{
|
||||||
|
return _define;
|
||||||
}
|
}
|
||||||
public static void AddData(string ind, object data)
|
public static void AddData(string ind, object data)
|
||||||
{
|
{
|
||||||
env[ind] = data;
|
_env[ind] = data;
|
||||||
}
|
}
|
||||||
public static void SetPM(parseManager o)
|
public static void SetPM(parseManager o)
|
||||||
{
|
{
|
||||||
current = o;
|
_current = o;
|
||||||
}
|
}
|
||||||
public static parseManager GetPM()
|
public static parseManager GetPM()
|
||||||
{
|
{
|
||||||
return current;
|
return _current;
|
||||||
|
}
|
||||||
|
public static void Add_Var(string var)
|
||||||
|
{
|
||||||
|
if (!_numvars.Contains(var)) {
|
||||||
|
_numvars.Add(var);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void Remove_Var(string var)
|
||||||
|
{
|
||||||
|
_numvars.Remove(var);
|
||||||
|
}
|
||||||
|
public static string[] GetVars()
|
||||||
|
{
|
||||||
|
return _numvars.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -790,4 +839,21 @@ public class standardDefine
|
|||||||
{
|
{
|
||||||
return a % b;
|
return a % b;
|
||||||
}
|
}
|
||||||
|
public double CALC(string ex)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -34,6 +34,9 @@
|
|||||||
<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>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user