Started to implement expressions
This commit is contained in:
parent
5a990e4313
commit
74324b86c3
@ -8,6 +8,7 @@
|
||||
*/
|
||||
using System;
|
||||
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 void testM(object arg1)
|
||||
@ -42,7 +43,8 @@ namespace parseManager
|
||||
{
|
||||
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;
|
||||
@ -56,7 +58,6 @@ namespace parseManager
|
||||
}
|
||||
next = test.Next();
|
||||
}
|
||||
//var temp=test.InvokeR("TEST",new object[]{});
|
||||
Console.Write("Press any key to continue . . . ");
|
||||
Console.ReadKey(true);
|
||||
}
|
||||
|
||||
@ -5,7 +5,9 @@ ENABLE leaking
|
||||
}
|
||||
[START]{
|
||||
"Test 1:"
|
||||
num=ADD(5,5)
|
||||
t=15
|
||||
test=2+t
|
||||
"test = $test$"
|
||||
"num = $num$"
|
||||
c=5
|
||||
::HERE::
|
||||
|
||||
@ -12,6 +12,7 @@ using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Reflection;
|
||||
using parseManager;
|
||||
using NCalc;
|
||||
namespace parseManager
|
||||
{
|
||||
/// <summary>
|
||||
@ -19,8 +20,8 @@ namespace parseManager
|
||||
/// </summary>
|
||||
public class parseManager
|
||||
{
|
||||
standardDefine _invoke = new standardDefine();
|
||||
string _filepath;
|
||||
bool _hasDefine;
|
||||
bool _active = true;
|
||||
string _define;
|
||||
string _entry = "START";
|
||||
@ -34,6 +35,27 @@ namespace parseManager
|
||||
Dictionary<string, bool> _flags = new Dictionary<string, bool>();
|
||||
Dictionary<string, chunk> _chunks = new Dictionary<string, chunk>();
|
||||
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()
|
||||
{
|
||||
_flags.Add("leaking", false);
|
||||
@ -41,7 +63,8 @@ namespace parseManager
|
||||
_flags.Add("debugging", false);
|
||||
_flags.Add("topdown", true);
|
||||
}
|
||||
public bool GetFlag(string flag){
|
||||
public bool GetFlag(string flag)
|
||||
{
|
||||
bool f;
|
||||
if (_flags.TryGetValue(flag, out f)) {
|
||||
return f;
|
||||
@ -104,12 +127,12 @@ namespace parseManager
|
||||
{
|
||||
_Parse(code);
|
||||
}
|
||||
public chunk[] GetChunks(){
|
||||
public chunk[] GetChunks()
|
||||
{
|
||||
var chunks = _chunks.Values;
|
||||
var temp = new chunk[_chunks.Count];
|
||||
var i = 0;
|
||||
foreach(var item in _chunks)
|
||||
{
|
||||
foreach (var item in _chunks) {
|
||||
temp[i] = item.Value;
|
||||
i++;
|
||||
}
|
||||
@ -123,40 +146,26 @@ namespace parseManager
|
||||
{
|
||||
_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)
|
||||
{ // TODO collect the returned arguments if any
|
||||
if (!_hasDefine)
|
||||
return null;
|
||||
{
|
||||
//try{
|
||||
_defineMethod = _defineType.GetMethod(method);
|
||||
return _defineMethod.Invoke(_defineClassObject, args);
|
||||
// } catch {
|
||||
// PushError("Invalid method: "+method);
|
||||
// return null;
|
||||
// }
|
||||
}
|
||||
public int InvokeNR(string method, object[] args)
|
||||
{ // Simple Invoking!
|
||||
if (!_hasDefine)
|
||||
return -1;
|
||||
{
|
||||
try {
|
||||
_defineMethod = _defineType.GetMethod(method);
|
||||
_defineMethod.Invoke(_defineClassObject, args);
|
||||
return 0;
|
||||
} catch {
|
||||
PushError("Invalid method: " + method);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
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 truths = new bool[conds.Length / 3];
|
||||
var c = 0;
|
||||
//Console.WriteLine(string.Join(",",conds));
|
||||
//Console.WriteLine(string.Join(",",andors));
|
||||
for (int i = 0; i < conds.Length; i += 3) {
|
||||
var condA = (object)ResolveVar(new []{ conds[i] })[0];
|
||||
var e = conds[i + 1];
|
||||
@ -326,15 +333,17 @@ namespace parseManager
|
||||
}
|
||||
var env = GetENV();
|
||||
env[retargs[0]] = data;
|
||||
GLOBALS.Add_Var(retargs[0]);
|
||||
tempReturn.SetCMDType("method");
|
||||
tempReturn.SetText("INVOKED METHOD: " + func);
|
||||
} else if (type == "ASSIGN") {
|
||||
} else if (type == "ASSIGN") { // TODO add lists/dictonaries support
|
||||
var vars = (string[])stuff[0];
|
||||
var vals = (string[])stuff[1];
|
||||
var env = GetENV();
|
||||
var types = ResolveVar(vals);
|
||||
for (int i = 0; i < types.Length; i++) {
|
||||
env[vars[i]] = types[i];
|
||||
GLOBALS.Add_Var(vars[i]);
|
||||
}
|
||||
tempReturn.SetCMDType("assignment");
|
||||
tempReturn.SetText(_currentChunk.GetLine());
|
||||
@ -426,12 +435,19 @@ namespace parseManager
|
||||
string temp;
|
||||
for (int i = 0; i < _lines.Length - 1; 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 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 logic = Regex.Match(temp, @"if\s*(.+)\s*then\s*(.+?\))\s*\|\s*(.+?\))");
|
||||
|
||||
if (logic.ToString() != "") {
|
||||
var condition = logic.Groups[1].ToString();
|
||||
var tempif = logic.Groups[2].ToString();
|
||||
@ -473,7 +489,14 @@ namespace parseManager
|
||||
conds[p++] = s1a2;
|
||||
conds[p++] = s1b2;
|
||||
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() != "") {
|
||||
_labels[label.Groups[1].ToString()] = i;
|
||||
_compiledlines.Add(new CMD("LABEL", new object[]{ }));
|
||||
@ -483,7 +506,11 @@ namespace parseManager
|
||||
var args = (FuncWReturn.Groups[3]).ToString();
|
||||
var retargs = var1.Split(',');
|
||||
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() != "") {
|
||||
var func = (FuncWOReturn.Groups[1]).ToString();
|
||||
var args = (FuncWOReturn.Groups[2]).ToString();
|
||||
@ -512,7 +539,8 @@ namespace parseManager
|
||||
_type = "CODEBLOCK";
|
||||
_clean(cont);
|
||||
}
|
||||
public string GetName(){
|
||||
public string GetName()
|
||||
{
|
||||
return _BLOCK;
|
||||
}
|
||||
public int GetLabel(string lab)
|
||||
@ -529,7 +557,8 @@ namespace parseManager
|
||||
pos = -1;
|
||||
return false;
|
||||
}
|
||||
public void RemoveNextChunk(){
|
||||
public void RemoveNextChunk()
|
||||
{
|
||||
_next = null;
|
||||
}
|
||||
public void SetNextChunk(chunk next)
|
||||
@ -705,23 +734,43 @@ namespace parseManager
|
||||
*/
|
||||
static class GLOBALS
|
||||
{
|
||||
static parseManager current;
|
||||
static readonly ENV env = new ENV();
|
||||
static standardDefine _define = new standardDefine();
|
||||
static parseManager _current;
|
||||
static readonly ENV _env = new ENV();
|
||||
static List<string> _numvars = new List<string>();
|
||||
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)
|
||||
{
|
||||
env[ind] = data;
|
||||
_env[ind] = data;
|
||||
}
|
||||
public static void SetPM(parseManager o)
|
||||
{
|
||||
current = o;
|
||||
_current = o;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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">
|
||||
<RequiredTargetFramework>4.0</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="NCalc">
|
||||
<HintPath>bin\Debug\NCalc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user