Initial Commit
This commit is contained in:
1118
Axiom/Axiom.Core/Interpreter/Assembler.cs
Executable file
1118
Axiom/Axiom.Core/Interpreter/Assembler.cs
Executable file
File diff suppressed because it is too large
Load Diff
21
Axiom/Axiom.Core/Interpreter/AxiomException.cs
Executable file
21
Axiom/Axiom.Core/Interpreter/AxiomException.cs
Executable file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
|
||||
// FileName : AxiomException.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class AxiomException : Exception
|
||||
{
|
||||
public AxiomResult AxiomResult{get;set;}
|
||||
public AxiomException()
|
||||
{
|
||||
}
|
||||
public AxiomException(AxiomResult axiomResult,Exception exception)
|
||||
: base(exception.Message,exception)
|
||||
{
|
||||
this.AxiomResult=axiomResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Axiom/Axiom.Core/Interpreter/AxiomResult.cs
Executable file
26
Axiom/Axiom.Core/Interpreter/AxiomResult.cs
Executable file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
|
||||
// FileName : AxiomResult.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class AxiomResult
|
||||
{
|
||||
public AxiomResult()
|
||||
{
|
||||
Success = false;
|
||||
}
|
||||
public AxiomResult(bool success,String lastMessage)
|
||||
{
|
||||
Success = success;
|
||||
LastMessage = lastMessage;
|
||||
}
|
||||
public bool Success { get; set; }
|
||||
public String LastMessage { get; set; }
|
||||
public DataTable DataTable { get; set; }
|
||||
public int ContextSpecificId{get;set;}
|
||||
public Object ContextSpecificObj { get; set; }
|
||||
}
|
||||
}
|
||||
45
Axiom/Axiom.Core/Interpreter/AxiomTrace.cs
Executable file
45
Axiom/Axiom.Core/Interpreter/AxiomTrace.cs
Executable file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
|
||||
// FileName : AxiomTrace.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class AxiomTrace
|
||||
{
|
||||
public AxiomTrace()
|
||||
{
|
||||
IsTracing = false;
|
||||
}
|
||||
public bool IsTracing { get; set; }
|
||||
public String[] Values { get; set; }
|
||||
public int Column { get; set; }
|
||||
|
||||
public static AxiomTrace FromConfig()
|
||||
{
|
||||
AxiomTrace trace = new AxiomTrace();
|
||||
try
|
||||
{
|
||||
return trace;
|
||||
// String tracingEnabled = ConfigurationManager.AppSettings["AXIOM_TRACE_ENABLED"];
|
||||
// if (null == tracingEnabled) return trace;
|
||||
// trace.IsTracing = Boolean.Parse(tracingEnabled);
|
||||
// trace.Column = int.Parse(ConfigurationManager.AppSettings["AXIOM_TRACE_COLUMN"]);
|
||||
// trace.Values = ConfigurationManager.AppSettings["AXIOM_TRACE_VALUES"].Split(',');
|
||||
// return trace;
|
||||
}
|
||||
catch (Exception /*exception*/)
|
||||
{
|
||||
return trace;
|
||||
}
|
||||
}
|
||||
public bool Break()
|
||||
{
|
||||
if (!Debugger.IsAttached) return false;
|
||||
Debugger.Break(); // if you wind up here you are in the debugger AND AXIOM_TRACE_ENABLED is set in the congifuration AND the break condition has been met. Step over this breakpoint to continue debugging OR set AXIOM_TRACE_ENABLED to false in config and restart
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
422
Axiom/Axiom.Core/Interpreter/CodeRunner.cs
Executable file
422
Axiom/Axiom.Core/Interpreter/CodeRunner.cs
Executable file
@@ -0,0 +1,422 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Data;
|
||||
using log4net;
|
||||
using Axiom.Utils;
|
||||
|
||||
// FileName : CodeRunner.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class CodeRunner : IDisposable
|
||||
{
|
||||
private static ILog logger = LogManager.GetLogger(typeof(CodeRunner));
|
||||
private SymbolTable symbolTable = null;
|
||||
private Dictionary<int, Stream> precompiles = new Dictionary<int, Stream>();
|
||||
private bool useCache=false;
|
||||
private bool trace=false;
|
||||
private bool parseStrict=false;
|
||||
private bool scanStrict=false;
|
||||
private bool disposed = false;
|
||||
private int parseSymbolCount=0;
|
||||
|
||||
public CodeRunner()
|
||||
{
|
||||
symbolTable=new SymbolTable();
|
||||
IsInError=false;
|
||||
}
|
||||
~CodeRunner()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if(disposing)
|
||||
{
|
||||
List<Stream> streams=new List<Stream>(precompiles.Values);
|
||||
foreach(Stream stream in streams)
|
||||
{
|
||||
try{stream.Close();stream.Dispose();}catch(Exception){;}
|
||||
}
|
||||
}
|
||||
disposed = true;
|
||||
}
|
||||
public int ParseSymbolsCount
|
||||
{
|
||||
get{return parseSymbolCount;}
|
||||
}
|
||||
public String LastMessage{ get; private set;}
|
||||
public bool IsInError { get; private set;}
|
||||
public bool ScanStrict
|
||||
{
|
||||
get{return scanStrict;}
|
||||
set{scanStrict=value;}
|
||||
}
|
||||
public bool ParseStrict
|
||||
{
|
||||
get{return parseStrict;}
|
||||
set{parseStrict=value;}
|
||||
}
|
||||
public bool Trace
|
||||
{
|
||||
get {return trace;}
|
||||
set {trace=value;}
|
||||
}
|
||||
public bool UseCache
|
||||
{
|
||||
get { return useCache; }
|
||||
set { useCache = value; }
|
||||
}
|
||||
public String GetValue(String name)
|
||||
{
|
||||
if (!symbolTable.ContainsKey(name)) return "null";
|
||||
Symbol symbol = symbolTable[name];
|
||||
GenericData genericData = symbol.GenericData;
|
||||
if (null == genericData || genericData.IsNull()) return "null";
|
||||
return genericData.Get<String>();
|
||||
}
|
||||
public T GetValue<T>(String name)
|
||||
{
|
||||
if(!symbolTable.ContainsKey(name)) return default(T);
|
||||
Symbol symbol=symbolTable[name];
|
||||
GenericData genericData=symbol.GenericData;
|
||||
if(null==genericData||genericData.IsNull()) return default(T);
|
||||
return genericData.Get<T>();
|
||||
}
|
||||
public SymbolTable SymbolTable
|
||||
{
|
||||
get {return symbolTable;}
|
||||
}
|
||||
public void Reset()
|
||||
{
|
||||
if (null == symbolTable) return;
|
||||
symbolTable.Reset();
|
||||
}
|
||||
public bool Execute(DataTable dataTable, int row, String expression)
|
||||
{
|
||||
Reset();
|
||||
return ExecuteExpressionOnRow(dataTable, row, expression);
|
||||
}
|
||||
private bool ExecuteExpressionOnRow(DataTable dataTable, int row, String expression)
|
||||
{
|
||||
symbolTable.AddUserSymbols(dataTable); // add symbol names from the data table columns
|
||||
symbolTable.AddUserValues(dataTable.Rows[row], dataTable);
|
||||
if (UseCache)
|
||||
{
|
||||
if (!ApplyRuleWithCache(expression)) return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Execute(expression))return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Execute(String expression)
|
||||
{
|
||||
BinaryReader binaryReader = null;
|
||||
BinaryWriter binaryWriter = null;
|
||||
BinaryReader parserReader = null;
|
||||
BinaryWriter parserWriter = null;
|
||||
BinaryReader assemblerReader = null;
|
||||
Assembler assembler = null;
|
||||
|
||||
try
|
||||
{
|
||||
IsInError=false;
|
||||
LastMessage="";
|
||||
binaryReader = new BinaryReader(Utility.StreamFromString(expression));
|
||||
binaryWriter = new BinaryWriter(new MemoryStream());
|
||||
Scanner scanner = new Scanner(binaryReader, binaryWriter, symbolTable);
|
||||
scanner.Debug = Trace;
|
||||
if (!scanner.Analyze())
|
||||
{
|
||||
LastMessage="Failed to scan the input document, possible invalid character sequence.";
|
||||
IsInError=true;
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return false;
|
||||
}
|
||||
binaryWriter.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
parserReader = new BinaryReader(binaryWriter.BaseStream);
|
||||
parserWriter = new BinaryWriter(new MemoryStream());
|
||||
Parser parser = new Parser(parserReader, parserWriter, symbolTable);
|
||||
parser.Debug = Trace;
|
||||
parser.Parse();
|
||||
parseSymbolCount=parser.ParseSymbolsCount;
|
||||
if (parser.IsInError)
|
||||
{
|
||||
LastMessage=String.Format("Failed to parse the input, {0} at {1}", parser.LastMessage, parser.LastLineNumber);
|
||||
IsInError=true;
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return false;
|
||||
}
|
||||
parserWriter.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
assemblerReader = new BinaryReader(parserWriter.BaseStream);
|
||||
assembler = new Assembler(assemblerReader, symbolTable);
|
||||
assembler.Debug = Trace;
|
||||
assembler.Assemble();
|
||||
if(assembler.IsInError)
|
||||
{
|
||||
LastMessage=String.Format("Error: Failed to run the assembler, {0}", assembler.LastMessage);
|
||||
IsInError=true;
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return false;
|
||||
}
|
||||
if (Trace) DumpSymbolTable();
|
||||
assembler.Dispose();
|
||||
assembler = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
LastMessage=String.Format("Exception:{0}",exception.ToString());
|
||||
IsInError=true;
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != binaryReader) { binaryReader.Close();binaryReader.Dispose(); }
|
||||
if (null != binaryWriter) { binaryWriter.Close();binaryWriter.Dispose(); }
|
||||
if (null != parserReader) { parserReader.Close();parserReader.Dispose(); }
|
||||
if (null != parserWriter) { parserWriter.Close(); parserWriter.Dispose(); }
|
||||
if (null != assemblerReader) { assemblerReader.Close(); assemblerReader.Dispose(); }
|
||||
if (null != assembler) { assembler.Dispose();assembler = null; }
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> Disassemble(String expression)
|
||||
{
|
||||
BinaryReader binaryReader = null;
|
||||
BinaryWriter binaryWriter = null;
|
||||
BinaryReader parserReader = null;
|
||||
BinaryWriter parserWriter = null;
|
||||
BinaryReader assemblerReader = null;
|
||||
Assembler assembler = null;
|
||||
List<String> disassembly=new List<String>();
|
||||
|
||||
try
|
||||
{
|
||||
IsInError=false;
|
||||
LastMessage="";
|
||||
binaryReader = new BinaryReader(Utility.StreamFromString(expression));
|
||||
binaryWriter = new BinaryWriter(new MemoryStream());
|
||||
Scanner scanner = new Scanner(binaryReader, binaryWriter, symbolTable);
|
||||
scanner.Debug = Trace;
|
||||
if (!scanner.Analyze())
|
||||
{
|
||||
LastMessage="Failed to scan the input document, possible invalid character sequence.";
|
||||
IsInError=true;
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return disassembly;
|
||||
}
|
||||
binaryWriter.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
parserReader = new BinaryReader(binaryWriter.BaseStream);
|
||||
parserWriter = new BinaryWriter(new MemoryStream());
|
||||
Parser parser = new Parser(parserReader, parserWriter, symbolTable);
|
||||
parser.Debug = Trace;
|
||||
parser.Parse();
|
||||
parseSymbolCount=parser.ParseSymbolsCount;
|
||||
if (parser.IsInError)
|
||||
{
|
||||
LastMessage=String.Format("Failed to parse the input, {0} at {1}", parser.LastMessage, parser.LastLineNumber);
|
||||
IsInError=true;
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return disassembly;
|
||||
}
|
||||
parserWriter.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
assemblerReader = new BinaryReader(parserWriter.BaseStream);
|
||||
assembler = new Assembler(assemblerReader, symbolTable);
|
||||
assembler.Debug = Trace;
|
||||
return assembler.Disassemble();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
LastMessage=String.Format("Exception:{0}",exception.ToString());
|
||||
IsInError=true;
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return disassembly;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != binaryReader) { binaryReader.Close();binaryReader.Dispose(); }
|
||||
if (null != binaryWriter) { binaryWriter.Close();binaryWriter.Dispose(); }
|
||||
if (null != parserReader) { parserReader.Close();parserReader.Dispose(); }
|
||||
if (null != parserWriter) { parserWriter.Close(); parserWriter.Dispose(); }
|
||||
if (null != assemblerReader) { assemblerReader.Close(); assemblerReader.Dispose(); }
|
||||
if (null != assembler) { assembler.Dispose();assembler = null; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// The cache will not work as effectively if the expression variables are not loaded directly into the SymbolTable
|
||||
public bool ApplyRuleWithCache(String expression)
|
||||
{
|
||||
BinaryReader assemblerReader = null;
|
||||
Stream precompiledStream = null;
|
||||
Assembler assembler = null;
|
||||
try
|
||||
{
|
||||
int hashcode = expression.GetHashCode();
|
||||
if (Trace) logger.InfoFormat("Trace:{0}", expression);
|
||||
if (precompiles.ContainsKey(hashcode))
|
||||
{
|
||||
precompiledStream = precompiles[hashcode]; // if the precompiled cache contains the expression then fetch it
|
||||
}
|
||||
else
|
||||
{
|
||||
precompiledStream = Compile(expression,true); // otherwise compile the expression and add it to the cache.
|
||||
precompiles.Add(hashcode, precompiledStream);
|
||||
}
|
||||
if (null == precompiledStream) return false;
|
||||
precompiledStream.Seek(0, SeekOrigin.Begin);
|
||||
assemblerReader = new BinaryReader(precompiledStream);
|
||||
assembler = new Assembler(assemblerReader, symbolTable);
|
||||
assembler.Assemble();
|
||||
if(assembler.IsInError)
|
||||
{
|
||||
LastMessage=String.Format("Error: Failed to run the assembler, {0}", assembler.LastMessage);
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return false;
|
||||
}
|
||||
assembler.Dispose();
|
||||
assembler = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
LastMessage=String.Format("Exception:{0}", exception.ToString());
|
||||
logger.ErrorFormat(LastMessage);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != assembler) { assembler.Dispose(); }
|
||||
}
|
||||
}
|
||||
public AxiomResult SyntaxCheck(String expression)
|
||||
{
|
||||
BinaryReader binaryReader = null;
|
||||
BinaryWriter binaryWriter = null;
|
||||
BinaryReader parserReader = null;
|
||||
BinaryWriter parserWriter = null;
|
||||
AxiomResult axiomResult = new AxiomResult();
|
||||
|
||||
try
|
||||
{
|
||||
axiomResult.Success = true;
|
||||
binaryWriter = new BinaryWriter(new MemoryStream());
|
||||
binaryReader = new BinaryReader(Utility.StreamFromString(expression));
|
||||
Scanner scanner = new Scanner(binaryReader, binaryWriter, symbolTable);
|
||||
if (!scanner.Analyze())
|
||||
{
|
||||
axiomResult.Success = false;
|
||||
axiomResult.LastMessage = "Failed to scan the input document, possible invalid character sequence.";
|
||||
return axiomResult;
|
||||
}
|
||||
binaryWriter.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
parserReader = new BinaryReader(binaryWriter.BaseStream);
|
||||
parserWriter = new BinaryWriter(new MemoryStream());
|
||||
Parser parser = new Parser(parserReader, parserWriter, symbolTable);
|
||||
// parser.StrictMode=false; // we do not require all variable declarations to be present.
|
||||
parser.StrictStatementMode=true; // we require at least one valid statement to be processed.
|
||||
parser.Parse();
|
||||
if (parser.IsInError)
|
||||
{
|
||||
axiomResult.Success = false;
|
||||
axiomResult.LastMessage = "Message:" + parser.LastMessage;
|
||||
return axiomResult;
|
||||
}
|
||||
return axiomResult;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
axiomResult.LastMessage = String.Format("Exception:{0}", exception.ToString());
|
||||
logger.Error(axiomResult.LastMessage);
|
||||
return axiomResult;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != binaryReader) { binaryReader.Close(); binaryReader.Dispose(); }
|
||||
if (null != binaryWriter) { binaryWriter.Close(); binaryWriter.Dispose(); }
|
||||
if (null != parserReader) { parserReader.Close(); parserReader.Dispose(); }
|
||||
if (null != parserWriter) { parserWriter.Close(); parserWriter.Dispose(); }
|
||||
}
|
||||
}
|
||||
private Stream Compile(String expression, bool includeCodeEnd = true)
|
||||
{
|
||||
BinaryReader binaryReader = null;
|
||||
BinaryWriter binaryWriter = null;
|
||||
BinaryReader parserReader = null;
|
||||
BinaryWriter parserWriter = null;
|
||||
|
||||
try
|
||||
{
|
||||
binaryReader = new BinaryReader(Utility.StreamFromString(expression));
|
||||
binaryWriter = new BinaryWriter(new MemoryStream());
|
||||
Scanner scanner = new Scanner(binaryReader, binaryWriter, symbolTable);
|
||||
scanner.Debug = Trace;
|
||||
if(!scanner.Analyze())
|
||||
{
|
||||
LastMessage = "Failed to scan the input document, possible invalid character sequence";
|
||||
logger.Info(LastMessage);
|
||||
return null;
|
||||
}
|
||||
binaryReader.Close();
|
||||
binaryReader.Dispose();
|
||||
binaryWriter.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
parserReader = new BinaryReader(binaryWriter.BaseStream);
|
||||
parserWriter = new BinaryWriter(new MemoryStream());
|
||||
Parser parser = new Parser(parserReader, parserWriter, symbolTable);
|
||||
parser.Debug = Trace;
|
||||
parser.Parse(includeCodeEnd);
|
||||
if (parser.IsInError)
|
||||
{
|
||||
LastMessage = String.Format("Error:{0} at {1}", parser.LastMessage, parser.LastLineNumber);
|
||||
logger.Info(LastMessage);
|
||||
return null;
|
||||
}
|
||||
parserReader.Close();
|
||||
parserReader.Dispose();
|
||||
parserReader = null;
|
||||
binaryWriter.Close();
|
||||
binaryWriter.Dispose();
|
||||
binaryWriter = null;
|
||||
parserWriter.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
return parserWriter.BaseStream;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.ErrorFormat("Exception:{0}", exception.ToString());
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != binaryReader) { binaryReader.Close(); binaryReader.Dispose(); }
|
||||
if (null != binaryWriter) { binaryWriter.Close(); binaryWriter.Dispose(); }
|
||||
if (null != parserReader) { parserReader.Close();parserReader.Dispose(); }
|
||||
}
|
||||
}
|
||||
private void DumpSymbolTable()
|
||||
{
|
||||
logger.Info("********************************************************* O U T P U T ************************************************");
|
||||
List<Symbol> list = new List<Symbol>(symbolTable.Values);
|
||||
list = (from Symbol symbol in list where symbol.TypeOfSymbol.Equals(Symbol.SymbolType.UserSymbol) select symbol).ToList();
|
||||
foreach (Symbol symbol in list)
|
||||
{
|
||||
logger.Info(String.Format("SYMBOL NAME:'{0}',VALUE:'{1}'", symbol.SymbolName, null == symbol.GenericData ? "<null>" : symbol.GenericData.ToString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
204
Axiom/Axiom.Core/Interpreter/Emitter.cs
Executable file
204
Axiom/Axiom.Core/Interpreter/Emitter.cs
Executable file
@@ -0,0 +1,204 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using log4net;
|
||||
|
||||
// FileName : Emitter.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class Emitter
|
||||
{
|
||||
private static ILog logger = LogManager.GetLogger(typeof(Emitter));
|
||||
private bool emitting = true;
|
||||
private int lastSymbol;
|
||||
private BinaryReader inputStream;
|
||||
private BinaryWriter outputStream;
|
||||
private bool debug = true;
|
||||
|
||||
public Emitter(BinaryReader inputStream, BinaryWriter outputStream)
|
||||
{
|
||||
this.inputStream = inputStream;
|
||||
this.outputStream = outputStream;
|
||||
}
|
||||
public bool Debug
|
||||
{
|
||||
get { return debug; }
|
||||
set { debug = value; }
|
||||
}
|
||||
public void Emit(String literalValue)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write(literalValue.Length);
|
||||
outputStream.Write(literalValue);
|
||||
}
|
||||
// ************************************************************************
|
||||
public void Emit(Scanner.ScanSymbols code)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write((int)code);
|
||||
if(Debug)logger.Info(Scanner.SymbolToString(code));
|
||||
}
|
||||
public void Emit(Scanner.ScanSymbols code,int value)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write((int)code);
|
||||
outputStream.Write(value);
|
||||
if(Debug)logger.Info(Scanner.SymbolToString(code)+","+value.ToString());
|
||||
}
|
||||
public void Emit(Scanner.ScanSymbols code,double value)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write((int)code);
|
||||
outputStream.Write(value);
|
||||
if(Debug)logger.Info(Scanner.SymbolToString(code)+","+value.ToString());
|
||||
}
|
||||
public void Emit(Scanner.ScanSymbols code,String value)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write((int)code);
|
||||
outputStream.Write(value);
|
||||
if(Debug)logger.Info(Scanner.SymbolToString(code)+","+value.ToString());
|
||||
}
|
||||
// **********************************************************************************************************************************************
|
||||
public long CodePointer()
|
||||
{
|
||||
return outputStream.BaseStream.Position;
|
||||
}
|
||||
public void Seek(long position)
|
||||
{
|
||||
outputStream.BaseStream.Seek(position, SeekOrigin.Begin);
|
||||
}
|
||||
public void Emit(Parser.ParserSymbols code)
|
||||
{
|
||||
if (!emitting) return;
|
||||
long positionBefore=outputStream.BaseStream.Position;
|
||||
outputStream.Write((int)code);
|
||||
long positionAfter=outputStream.BaseStream.Position;
|
||||
if(Debug)logger.Info(Parser.SymbolToString(code)+"["+positionBefore+","+positionAfter+"]");
|
||||
|
||||
}
|
||||
public void Emit(Parser.ParserSymbols code,Object value)
|
||||
{
|
||||
if (!emitting) return;
|
||||
long positionBefore=outputStream.BaseStream.Position;
|
||||
outputStream.Write((int)code);
|
||||
Type type = value.GetType();
|
||||
outputStream.Write(type.ToString());
|
||||
outputStream.Write(value.ToString());
|
||||
long positionAfter=outputStream.BaseStream.Position;
|
||||
if(Debug)logger.Info(Parser.SymbolToString(code)+","+type.ToString()+","+value.ToString()+"["+positionBefore+","+positionAfter+"]");
|
||||
}
|
||||
public void Emit(Parser.ParserSymbols code,Object value,int intValue)
|
||||
{
|
||||
if (!emitting) return;
|
||||
long positionBefore=outputStream.BaseStream.Position;
|
||||
outputStream.Write((int)code);
|
||||
Type type = value.GetType();
|
||||
outputStream.Write(type.ToString());
|
||||
outputStream.Write(value.ToString());
|
||||
outputStream.Write(intValue);
|
||||
long positionAfter=outputStream.BaseStream.Position;
|
||||
if(Debug)logger.Info(Parser.SymbolToString(code)+","+type.ToString()+","+value.ToString()+","+intValue+"["+positionBefore+","+positionAfter+"]");
|
||||
}
|
||||
public void Emit(Parser.ParserSymbols code,long value)
|
||||
{
|
||||
if (!emitting) return;
|
||||
long positionBefore=outputStream.BaseStream.Position;
|
||||
outputStream.Write((int)code);
|
||||
outputStream.Write(value);
|
||||
long positionAfter=outputStream.BaseStream.Position;
|
||||
if(Debug)logger.Info(Parser.SymbolToString(code)+","+value.ToString()+","+value.ToString()+"["+positionBefore+","+positionAfter+"]");
|
||||
}
|
||||
public void EmitAsNull(Parser.ParserSymbols code)
|
||||
{
|
||||
if (!emitting) return;
|
||||
long positionBefore=outputStream.BaseStream.Position;
|
||||
outputStream.Write((int)code);
|
||||
Type type = typeof(System.Nullable); //value.GetType();
|
||||
outputStream.Write(type.ToString());
|
||||
outputStream.Write("null".ToString());
|
||||
long positionAfter=outputStream.BaseStream.Position;
|
||||
if(Debug)logger.Info(Parser.SymbolToString(code)+","+type.ToString()+","+"null".ToString()+"["+positionBefore+","+positionAfter+"]");
|
||||
}
|
||||
// ************************************************************************
|
||||
public void Emit(int code, int op)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write(code);
|
||||
outputStream.Write(op);
|
||||
}
|
||||
public void Emit(int identifier)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write(identifier);
|
||||
}
|
||||
public void Emit(byte value)
|
||||
{
|
||||
if (!emitting) return;
|
||||
outputStream.Write(value);
|
||||
}
|
||||
public int Peek(ref int value)
|
||||
{
|
||||
value = inputStream.PeekChar();
|
||||
return value;
|
||||
}
|
||||
public int Peek()
|
||||
{
|
||||
int value = inputStream.PeekChar();
|
||||
return value;
|
||||
}
|
||||
|
||||
public int PeekIgnore(ref int value,int[] ignoreChars)
|
||||
{
|
||||
long streamPosition=inputStream.BaseStream.Position;
|
||||
while(true)
|
||||
{
|
||||
int readValue;
|
||||
readValue=(int)inputStream.ReadChar();
|
||||
value=readValue;
|
||||
if(!ignoreChars.Any(x=>x.Equals(readValue)))break;
|
||||
}
|
||||
inputStream.BaseStream.Seek(streamPosition,SeekOrigin.Begin);
|
||||
return value;
|
||||
}
|
||||
public int Read()
|
||||
{
|
||||
lastSymbol = inputStream.Read();
|
||||
return lastSymbol;
|
||||
}
|
||||
public int Read(ref String literal)
|
||||
{
|
||||
literal=inputStream.ReadString();
|
||||
return 0;
|
||||
}
|
||||
public int Read(ref byte value)
|
||||
{
|
||||
try { value = inputStream.ReadByte(); return 0; }
|
||||
catch (EndOfStreamException) { return 0xFFFF; }
|
||||
}
|
||||
public int Read(ref int value)
|
||||
{
|
||||
try { value = inputStream.ReadInt32(); return 0; }
|
||||
catch (EndOfStreamException) { return 0xFFFF; }
|
||||
|
||||
}
|
||||
public int Read(ref double value)
|
||||
{
|
||||
try { value = inputStream.ReadDouble(); return 0; }
|
||||
catch (EndOfStreamException) { return 0xFFFF; }
|
||||
}
|
||||
public bool Emitting
|
||||
{
|
||||
get
|
||||
{
|
||||
return emitting;
|
||||
}
|
||||
set
|
||||
{
|
||||
emitting = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
713
Axiom/Axiom.Core/Interpreter/GenericData.cs
Executable file
713
Axiom/Axiom.Core/Interpreter/GenericData.cs
Executable file
@@ -0,0 +1,713 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using log4net;
|
||||
//using MarketData.DataAccess;
|
||||
//using MarketData.MarketDataModel;
|
||||
using System.Diagnostics;
|
||||
using Axiom.Utils;
|
||||
|
||||
// FileName : GenericData.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class GenericData
|
||||
{
|
||||
private static ILog logger = LogManager.GetLogger(typeof(GenericData));
|
||||
private Object objData = null;
|
||||
private String objDataType = "";
|
||||
private bool isModified=false;
|
||||
|
||||
public Object Data
|
||||
{
|
||||
set
|
||||
{
|
||||
objData = value;
|
||||
if(null!=objData)objDataType =objData.GetType().ToString();
|
||||
}
|
||||
get
|
||||
{
|
||||
return objData;
|
||||
}
|
||||
}
|
||||
public bool IsNull()
|
||||
{
|
||||
return null == objData ? true : false;
|
||||
}
|
||||
public bool IsModified
|
||||
{
|
||||
get {return isModified;}
|
||||
set {isModified=value;}
|
||||
}
|
||||
public static GenericData PerformConvert(GenericData data1, GenericData data2) // convert data of param1 to the type described in data2
|
||||
{
|
||||
GenericData genericData = new GenericData();
|
||||
genericData.SetData(data1.GetStringRepData(),data2.Get<String>());
|
||||
return genericData;
|
||||
}
|
||||
public GenericData GetPrice(GenericData dateData,GenericData opData)
|
||||
{
|
||||
GenericData resultData = new GenericData();
|
||||
double result=default(double);
|
||||
try
|
||||
{
|
||||
String strSymbol=this.Get<String>();
|
||||
DateTime date = dateData.Get<DateTime>();
|
||||
// Price price=PricingDA.GetPrice(strSymbol,date);
|
||||
// if(null != price)
|
||||
// {
|
||||
// String strOperation=opData.Get<String>().ToUpper();
|
||||
// if(strOperation.Equals("OPEN"))result = price.Open;
|
||||
// else if(strOperation.Equals("HIGH"))result = price.High;
|
||||
// else if(strOperation.Equals("LOW"))result = price.Low;
|
||||
// else result = price.Close;
|
||||
// }
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Debug.WriteLine(exception.ToString());
|
||||
}
|
||||
resultData.SetData(result);
|
||||
return resultData;
|
||||
}
|
||||
public GenericData Substring(GenericData startIndexData,GenericData lengthData)
|
||||
{
|
||||
GenericData resultData = new GenericData();
|
||||
String result = null;
|
||||
try
|
||||
{
|
||||
int startIndex = startIndexData.Get<int>();
|
||||
int length = lengthData.Get<int>();
|
||||
String strArgument = this.Get<String>();
|
||||
result = strArgument.Substring(startIndex - 1, length);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
resultData.SetData(result);
|
||||
return resultData;
|
||||
}
|
||||
public GenericData Trim()
|
||||
{
|
||||
GenericData resultData = new GenericData();
|
||||
String result = null;
|
||||
try
|
||||
{
|
||||
String strArgument = this.Get<String>();
|
||||
result = strArgument.Trim();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
resultData.SetData(result);
|
||||
return resultData;
|
||||
}
|
||||
public GenericData Upper()
|
||||
{
|
||||
GenericData resultData = new GenericData();
|
||||
String result = null;
|
||||
try
|
||||
{
|
||||
String strArgument = this.Get<String>();
|
||||
result = strArgument.ToUpper();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
resultData.SetData(result);
|
||||
return resultData;
|
||||
}
|
||||
public GenericData Lower()
|
||||
{
|
||||
GenericData resultData = new GenericData();
|
||||
String result = null;
|
||||
try
|
||||
{
|
||||
String strArgument = this.Get<String>();
|
||||
result = strArgument.ToLower();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
resultData.SetData(result);
|
||||
return resultData;
|
||||
}
|
||||
public GenericData Like(GenericData argument)
|
||||
{
|
||||
GenericData genericData = new GenericData();
|
||||
String thisString = this.Get<String>();
|
||||
String strArgument = argument.GetStringRepData();
|
||||
if (null == thisString || null == strArgument)
|
||||
{
|
||||
genericData.SetData(false);
|
||||
return genericData;
|
||||
}
|
||||
strArgument = strArgument.Replace("%", null);
|
||||
genericData.SetData(thisString.Contains(strArgument));
|
||||
return genericData;
|
||||
}
|
||||
public void SetNull()
|
||||
{
|
||||
objDataType="System.Nullable";
|
||||
objData=Convert.ChangeType(null,Type.GetType(objDataType));
|
||||
}
|
||||
public void SetNull(String strDataType)
|
||||
{
|
||||
objDataType=GetTypeString(strDataType);
|
||||
objData=Convert.ChangeType(null,Type.GetType(objDataType));
|
||||
}
|
||||
public void SetData(double data)
|
||||
{
|
||||
Type targetType = data.GetType();
|
||||
objDataType = targetType.Name;
|
||||
objData = data;
|
||||
}
|
||||
public void SetData(bool data)
|
||||
{
|
||||
Type targetType = data.GetType();
|
||||
objDataType = targetType.Name;
|
||||
objData = data;
|
||||
}
|
||||
public void SetData(String data)
|
||||
{
|
||||
Type targetType = data.GetType();
|
||||
objDataType = targetType.Name;
|
||||
objData = data;
|
||||
}
|
||||
public void SetData(String stringRep, String typeName)
|
||||
{
|
||||
Type targetType = Type.GetType(typeName);
|
||||
objDataType = typeName;
|
||||
objData = Convert.ChangeType(stringRep, targetType);
|
||||
}
|
||||
public static GenericData Clone(GenericData genericData)
|
||||
{
|
||||
GenericData copyGenericData = new GenericData();
|
||||
if (null == genericData) return copyGenericData;
|
||||
copyGenericData.SetData(genericData.GetStringRepData(),genericData.GetStringRepType());
|
||||
return copyGenericData;
|
||||
}
|
||||
// *************************************************************************************************************************************************************************************
|
||||
// *********************************************************************************** L O G I C A L O P E R A T I O N S *************************************************************
|
||||
// *************************************************************************************************************************************************************************************
|
||||
public GenericData Not()
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
String stringRepType = GetStringRepType();
|
||||
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
else if ("System.Boolean".Equals(stringRepType))
|
||||
{
|
||||
data.Data=!(Get<Boolean>());
|
||||
}
|
||||
else if ("System.Int32".Equals(stringRepType))
|
||||
{
|
||||
data.Data = (Get<Int32>() == 0);
|
||||
}
|
||||
else if ("System.String".Equals(stringRepType))
|
||||
{
|
||||
data.Data = !(Get<Boolean>());
|
||||
}
|
||||
else if ("System.Double".Equals(stringRepType))
|
||||
{
|
||||
data.Data = (Get<double>() == 0.00);
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData LessEqual(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<double>()<=genericData.Get<double>();
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<Int32>()<=genericData.Get<Int32>();
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<String>().Length<=genericData.Get<String>().Length;
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData Or(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = (0.00!=Get<double>())||(0.00!=genericData.Get<double>());
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = (0!=Get<Int32>())||(0!=genericData.Get<Int32>());
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Boolean".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<Boolean>()||genericData.Get<Boolean>();
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData And(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = (0.00 != Get<double>()) && (0.00 != genericData.Get<double>());
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = (0 != Get<Int32>()) && (0 != genericData.Get<Int32>());
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Boolean".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<Boolean>() && genericData.Get<Boolean>();
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData Greater(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<double>()>genericData.Get<double>();
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.DateTime".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<DateTime>()>genericData.Get<DateTime>();
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<Int32>()>genericData.Get<Int32>();
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<String>().Length>genericData.Get<String>().Length;
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData GreaterEqual(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<double>()>=genericData.Get<double>();
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<Int32>()>=genericData.Get<Int32>();
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<String>().Length>=genericData.Get<String>().Length;
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData EqualEqual(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
|
||||
if (IsNull() && genericData.IsNull())
|
||||
{
|
||||
data.SetData(true);
|
||||
}
|
||||
else if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = (0 == Get<double>().CompareTo(genericData.Get<double>())); }
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = (0 == Get<Int32>().CompareTo(genericData.Get<Int32>())); }
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = (Get<String>().Equals(genericData.Get<String>()));
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData NotEqual(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull() && !genericData.IsNull())
|
||||
{
|
||||
data.SetData(true);
|
||||
}
|
||||
else if (!IsNull() && genericData.IsNull())
|
||||
{
|
||||
data.SetData(true);
|
||||
}
|
||||
else if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = (0 != Get<double>().CompareTo(genericData.Get<double>())); }
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = (0 != Get<Int32>().CompareTo(genericData.Get<Int32>())); }
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = !(Get<String>().Equals(genericData.Get<String>()));
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData Less(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetData(false);
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = (-1==Get<double>().CompareTo(genericData.Get<double>())); }
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = (-1==Get<Int32>().CompareTo(genericData.Get<Int32>())); }
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<String>().Equals(genericData.Get<String>());
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
// **************************************************************************************************************************************************************************************
|
||||
// ********************************************************************************** M A T H E M A T I C A L O P E R A T I O N S ****************************************************
|
||||
// **************************************************************************************************************************************************************************************
|
||||
public GenericData Negate()
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
String stringRepType = GetStringRepType();
|
||||
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
else if ("System.Double".Equals(stringRepType))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<double>() * -1.00;
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Get<Int32>() * -1;
|
||||
}
|
||||
catch (Exception){data.SetNull();}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
public GenericData Abs()
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
String stringRepType = GetStringRepType();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
if ("System.Double".Equals(stringRepType))
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Math.Abs(Get<double>());
|
||||
}
|
||||
catch (Exception) { data.Data = 0.00; }
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
data.Data = Math.Abs(Get<Int32>());
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public GenericData Multiply(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = Get<double>() * genericData.Get<double>(); }
|
||||
catch (Exception) { data.Data = double.NaN; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = Get<Int32>() * genericData.Get<Int32>(); }
|
||||
catch (Exception) { data.Data = Int32.MinValue; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = Get<double>() * genericData.Get<double>(); } // genericData is a string but try to cast and perform the multiplication anyway
|
||||
catch (Exception) { data.Data = double.NaN; }
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData Divide(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = Get<double>() / genericData.Get<double>(); }
|
||||
catch (Exception) { data.Data = double.NaN; }
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
try { data.Data = Get<Int32>() / genericData.Get<Int32>(); }
|
||||
catch (Exception) { data.Data = Int32.MinValue; }
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = double.NaN;
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData Add(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<double>()+genericData.Get<double>();
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<Int32>()+genericData.Get<Int32>();
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<String>() + genericData.Get<String>();
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
public GenericData Subtract(GenericData genericData)
|
||||
{
|
||||
GenericData data = new GenericData();
|
||||
if (IsNull())
|
||||
{
|
||||
data.SetNull();
|
||||
}
|
||||
else if ("System.Double".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<double>() - genericData.Get<double>();
|
||||
}
|
||||
else if ("System.Int32".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = Get<Int32>() - genericData.Get<Int32>();
|
||||
}
|
||||
else if ("System.String".Equals(genericData.GetStringRepType()))
|
||||
{
|
||||
data.Data = (Get<String>()).Replace(genericData.Get<String>(), null);
|
||||
}
|
||||
else data.SetNull();
|
||||
return data;
|
||||
}
|
||||
// ********************************************************************************************************** E N D O P E R A T I O N S ********************************************************************************************
|
||||
public String GetStringRepData()
|
||||
{
|
||||
if (null == objData) return null;
|
||||
String stringRepType = GetStringRepType();
|
||||
return (String)Convert.ChangeType(objData, typeof(String));
|
||||
}
|
||||
public String GetStringRepType()
|
||||
{
|
||||
if (null == objData) return "System.Nullable";
|
||||
return objData.GetType().ToString();
|
||||
}
|
||||
public Type DataType
|
||||
{
|
||||
get
|
||||
{
|
||||
if (null == objData) throw new Exception("No type data for null value.");
|
||||
return objData.GetType();
|
||||
}
|
||||
}
|
||||
public override String ToString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append(GetStringRepType()).Append(":").Append("'").Append(GetStringRepData()).Append("'");
|
||||
return sb.ToString();
|
||||
}
|
||||
public T Get<T>()
|
||||
{
|
||||
T result = default(T);
|
||||
try
|
||||
{
|
||||
result = (T)Convert.ChangeType(objData, typeof(T));
|
||||
}
|
||||
catch(Exception /*exception*/)
|
||||
{
|
||||
result = default(T);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// **********************************************************************************************************************************************************************************
|
||||
// ************************************************************************ P U B L I C S T A T I C H E L P E R S ****************************************************************
|
||||
// **********************************************************************************************************************************************************************************
|
||||
public static Object ConvertType(String destinationType,String stringRep)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (destinationType.Equals("System.DBNull")) destinationType = "System.String";
|
||||
Type targetType = Type.GetType(destinationType);
|
||||
String objDataType = destinationType;
|
||||
Object objData = Convert.ChangeType(stringRep, targetType);
|
||||
return objData;
|
||||
}
|
||||
catch(Exception /*exception*/)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static Object ConvertType(Type targetType,String stringRep)
|
||||
{
|
||||
try
|
||||
{
|
||||
Object objData = null;
|
||||
String stringRepType = targetType.ToString();
|
||||
if(targetType.Equals("System.Double")&&null==stringRep||stringRep.Equals(""))return objData;
|
||||
else if (stringRepType.Equals("System.DBNull")) stringRep = "System.String";
|
||||
objData = Convert.ChangeType(stringRep, targetType);
|
||||
return objData;
|
||||
}
|
||||
catch(Exception /*exception*/)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static Type GetType(String strType)
|
||||
{
|
||||
strType=strType.ToUpper();
|
||||
if(strType.Equals("STRING")||strType.Equals("SYSTEM.STRING"))return Type.GetType("System.String");
|
||||
else if(strType.Equals("NUMERIC")||strType.Equals("DOUBLE")||strType.Equals("SYSTEM.DOUBLE"))return Type.GetType("System.Double");
|
||||
else if(strType.Equals("DATETIME")||strType.Equals("SYSTEM.DATETIME"))return Type.GetType("System.DateTime");
|
||||
return Type.GetType("System.String");
|
||||
}
|
||||
public static String GetTypeString(String strType)
|
||||
{
|
||||
strType=strType.ToUpper();
|
||||
if(strType.Equals("STRING")||strType.Equals("SYSTEM.STRING"))return "System.String";
|
||||
else if(strType.Equals("NUMERIC")||strType.Equals("DOUBLE")||strType.Equals("SYSTEM.DOUBLE"))return "System.Double";
|
||||
else if(strType.Equals("DATETIME")||strType.Equals("SYSTEM.DATETIME"))return "System.DateTime";
|
||||
return "System.String";
|
||||
}
|
||||
public static String ToExpressionData(System.Data.DataColumn dataColumn,String value)
|
||||
{
|
||||
if (dataColumn.DataType.Name.ToUpper().Equals("STRING")) return "'"+Utility.SqlString(value)+"'";
|
||||
if (dataColumn.DataType.Name.ToUpper().Equals("DATETIME")) return "'"+Utility.SqlString(value)+"'";
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
91
Axiom/Axiom.Core/Interpreter/ParseSymbols.cs
Executable file
91
Axiom/Axiom.Core/Interpreter/ParseSymbols.cs
Executable file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
// FileName : ParseSymbols.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class ParseSymbols : List<ParseSymbol>
|
||||
{
|
||||
private Dictionary<Scanner.ScanSymbols, Scanner.ScanSymbols> uniqueSymbols = new Dictionary<Scanner.ScanSymbols, Scanner.ScanSymbols>();
|
||||
|
||||
public ParseSymbols()
|
||||
{
|
||||
}
|
||||
public ParseSymbols(List<ParseSymbol> symbols)
|
||||
{
|
||||
foreach (ParseSymbol symbol in symbols) InsertSymbols(symbol);
|
||||
}
|
||||
public new void Add(ParseSymbol parseSymbol)
|
||||
{
|
||||
InsertSymbols(parseSymbol);
|
||||
}
|
||||
public void InsertSymbols(ParseSymbols symbols)
|
||||
{
|
||||
foreach (ParseSymbol parseSymbol in symbols)
|
||||
{
|
||||
base.Add(parseSymbol);
|
||||
if (!uniqueSymbols.ContainsKey(parseSymbol.Symbol)) uniqueSymbols.Add(parseSymbol.Symbol,parseSymbol.Symbol);
|
||||
}
|
||||
}
|
||||
public void InsertSymbols(ParseSymbol parseSymbol)
|
||||
{
|
||||
base.Add(parseSymbol);
|
||||
if (!uniqueSymbols.ContainsKey(parseSymbol.Symbol)) uniqueSymbols.Add(parseSymbol.Symbol,parseSymbol.Symbol);
|
||||
}
|
||||
public void RemoveSymbols(ParseSymbols symbols)
|
||||
{
|
||||
for (int index = symbols.Count - 1; index >= 0;index--)
|
||||
{
|
||||
ParseSymbol symbol = symbols[index];
|
||||
base.Remove(symbol);
|
||||
int count = (from ParseSymbol parseSymbol in this where parseSymbol.Symbol.Equals(symbol.Symbol) select parseSymbol).Count();
|
||||
if (0 == count && uniqueSymbols.ContainsKey(symbol.Symbol)) uniqueSymbols.Remove(symbol.Symbol);
|
||||
}
|
||||
}
|
||||
public void RemoveSymbols(ParseSymbol symbol)
|
||||
{
|
||||
base.Remove(symbol);
|
||||
int count = (from ParseSymbol parseSymbol in this where parseSymbol.Symbol.Equals(symbol.Symbol) select parseSymbol).Count();
|
||||
if(0==count&&uniqueSymbols.ContainsKey(symbol.Symbol))uniqueSymbols.Remove(symbol.Symbol);
|
||||
}
|
||||
public bool SymbolIn(Scanner.ScanSymbols symbol)
|
||||
{
|
||||
return uniqueSymbols.ContainsKey(symbol);
|
||||
}
|
||||
public bool SymbolIn(ParseSymbols parseSymbols)
|
||||
{
|
||||
foreach (ParseSymbol symbol in parseSymbols) if (uniqueSymbols.ContainsKey(symbol.Symbol)) return true;
|
||||
return false;
|
||||
}
|
||||
public bool SymbolIn(ParseSymbol parseSymbol)
|
||||
{
|
||||
return uniqueSymbols.ContainsKey(parseSymbol.Symbol);
|
||||
}
|
||||
}
|
||||
public class ParseSymbol : IEquatable<ParseSymbol>
|
||||
{
|
||||
private Scanner.ScanSymbols symbol;
|
||||
public ParseSymbol()
|
||||
{
|
||||
}
|
||||
public ParseSymbol(Scanner.ScanSymbols symbol)
|
||||
{
|
||||
this.symbol = symbol;
|
||||
}
|
||||
public Scanner.ScanSymbols Symbol
|
||||
{
|
||||
get
|
||||
{
|
||||
return symbol;
|
||||
}
|
||||
}
|
||||
public bool Equals(ParseSymbol parseSymbol)
|
||||
{
|
||||
if (parseSymbol == null) return false;
|
||||
return Symbol.Equals(parseSymbol.Symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
1756
Axiom/Axiom.Core/Interpreter/Parser.cs
Executable file
1756
Axiom/Axiom.Core/Interpreter/Parser.cs
Executable file
File diff suppressed because it is too large
Load Diff
625
Axiom/Axiom.Core/Interpreter/Scanner.cs
Executable file
625
Axiom/Axiom.Core/Interpreter/Scanner.cs
Executable file
@@ -0,0 +1,625 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
// FileName : ParseSymbols.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class Scanner : Emitter
|
||||
{
|
||||
public enum ScanSymbols { unknown1, directive_clear_modified1,declare1, null1, isnull1, convert1, getprice1, substring1, in1, like1, trim1, upper1, lower1, assign1, if1, while1, then1,else1,goto1,equal1,equalequal1,less1,lessequal1,greater1,greaterequal1, notequal1,variable1, asterisk1, apostrophe1, comma1, label1, literal1, leftcurly1, rightcurly1, leftbracket1, rightbracket1, numeral1, char1, divide1, plus1, minus1, leftparen1, rightparen1, newline1, semicolon1,endtext1, andand1, oror1, abs1, not1, pow1, sqrt1, break1, end1 };
|
||||
private enum WhiteSpace{spacechar=32,tabchar=9};
|
||||
private int character;
|
||||
private StringBuilder word;
|
||||
private SymbolTable symbolTable;
|
||||
|
||||
public Scanner(BinaryReader binaryReader, BinaryWriter binaryWriter,SymbolTable symbolTable)
|
||||
: base(binaryReader, binaryWriter)
|
||||
{
|
||||
this.symbolTable = symbolTable;
|
||||
}
|
||||
public bool Analyze()
|
||||
{
|
||||
if(!ReadCh())return false;
|
||||
while ((int)character != -1 && character != 0x001A)
|
||||
{
|
||||
SkipSeparators();
|
||||
if(-1==(int)character)break;
|
||||
if (0xFFFF == character || 0x001A == character) break;
|
||||
if (IsDigit(character) || '.'.Equals((char)character)) ScanNumeral();
|
||||
else if ('\r' == character) ScanNewLine();
|
||||
else if (';' == character) { Emit(ScanSymbols.semicolon1); ReadCh(); }
|
||||
else if (',' == character) { Emit(ScanSymbols.comma1); ReadCh(); }
|
||||
else if ('[' == character) { Emit(ScanSymbols.leftbracket1); ReadCh(); }
|
||||
else if (']' == character) { Emit(ScanSymbols.rightbracket1); ReadCh(); }
|
||||
else if ('*' == character) { Emit(ScanSymbols.asterisk1); ReadCh(); }
|
||||
else if ('/' == character) { ScanDivide(); }
|
||||
else if ('+' == character) { Emit(ScanSymbols.plus1); ReadCh(); }
|
||||
else if ('-' == character) { Emit(ScanSymbols.minus1); ReadCh(); }
|
||||
else if ('(' == character) { Emit(ScanSymbols.leftparen1); ReadCh(); }
|
||||
else if (')' == character) { Emit(ScanSymbols.rightparen1); ReadCh(); }
|
||||
else if ('!' == character) { Emit(ScanSymbols.not1); ReadCh(); }
|
||||
else if ('{' == character) { Emit(ScanSymbols.leftcurly1); ReadCh(); }
|
||||
else if ('}' == character) { Emit(ScanSymbols.rightcurly1); ReadCh(); }
|
||||
else if ('\'' == character) ScanLiteral();
|
||||
else if (';' == character) ScanComment();
|
||||
else if ('=' == character) ScanEqual();
|
||||
else if ('<' == character) ScanLess();
|
||||
else if ('>' == character) ScanGreater();
|
||||
else if ('&' == character) ScanAnd();
|
||||
else if ('|' == character) ScanOr();
|
||||
else if (IsAlpha(character))
|
||||
{
|
||||
if (!ScanWord()) return false;
|
||||
}
|
||||
else if ('#' == character)
|
||||
{
|
||||
if (!ScanDirective()) return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
ScanUnknown();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Emit(Scanner.ScanSymbols.endtext1);
|
||||
return true;
|
||||
}
|
||||
public bool ReadCh()
|
||||
{
|
||||
try
|
||||
{
|
||||
character = Read();
|
||||
if (-1 == character) return false;
|
||||
return true;
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
character = -1;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void ScanDivide()
|
||||
{
|
||||
Emit(ScanSymbols.divide1);
|
||||
ReadCh();
|
||||
}
|
||||
public bool PeekCh(ref char ch)
|
||||
{
|
||||
try
|
||||
{
|
||||
int character=Peek();
|
||||
if(-1==character)return false;
|
||||
ch=(Char)character;
|
||||
return true;
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool ScanDirective()
|
||||
{
|
||||
char nextChar='\0';
|
||||
if(!PeekCh(ref nextChar))return false;
|
||||
if(!nextChar.Equals('#'))return ScanWord();
|
||||
while (-1!=character && 0x0D != character && 0xFFFF != character)ReadCh(); // if we have ## then take the line as a comment
|
||||
return true;
|
||||
}
|
||||
public bool ScanWord()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (-1!=character && 0x0D != character && !IsKeySymbol() && 0xFFFF != character && (int)WhiteSpace.spacechar!=character && (int)WhiteSpace.tabchar != character)
|
||||
{
|
||||
sb.Append((char)character);
|
||||
ReadCh();
|
||||
}
|
||||
if (0 == sb.Length) return true;
|
||||
String symbolName = sb.ToString();
|
||||
if (':'.Equals((char)character))
|
||||
{
|
||||
Emit(ScanSymbols.label1);
|
||||
Emit(symbolName);
|
||||
ReadCh();
|
||||
}
|
||||
else if(symbolTable.ContainsKey(symbolName))
|
||||
{
|
||||
Symbol symbol = symbolTable[symbolName];
|
||||
if (Symbol.SymbolType.FunctionSymbol.Equals(symbol.TypeOfSymbol)) Emit(symbol.Identifier);
|
||||
else if(Symbol.SymbolType.KeywordSymbol.Equals(symbol.TypeOfSymbol)) Emit(symbol.Identifier);
|
||||
else if(Symbol.SymbolType.DirectiveSymbol.Equals(symbol.TypeOfSymbol)) Emit(symbol.Identifier);
|
||||
else Emit(ScanSymbols.variable1, symbol.SymbolName);
|
||||
}
|
||||
else if(symbolTable.ContainsKey(symbolName.ToLower()))
|
||||
{
|
||||
symbolName = symbolName.ToLower();
|
||||
Symbol symbol = symbolTable[symbolName];
|
||||
if (Symbol.SymbolType.FunctionSymbol.Equals(symbol.TypeOfSymbol)) Emit(symbol.Identifier);
|
||||
else if(Symbol.SymbolType.KeywordSymbol.Equals(symbol.TypeOfSymbol)) Emit(symbol.Identifier);
|
||||
else Emit(ScanSymbols.variable1, symbol.SymbolName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Emit(ScanSymbols.variable1, symbolName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool ScanNumeral()
|
||||
{
|
||||
int[] chBuffer = new int[128];
|
||||
int chIndex = 0;
|
||||
while (0xFFFF != character && (IsDigit(character) || IsInHex(character) || '.'.Equals((char)character)))
|
||||
{
|
||||
if (chIndex >= chBuffer.Length) return false;
|
||||
chBuffer[chIndex++] = character;
|
||||
ReadCh();
|
||||
}
|
||||
if (character == 69) Exponent(chBuffer, chIndex);
|
||||
else if ('h' == character || 'H' == character) { Hex(chBuffer, chIndex); ReadCh(); }
|
||||
else if ('b' == character || 'B' == character) { Binary(chBuffer, chIndex); ReadCh(); }
|
||||
else if (chIndex > 0 && ('b' == chBuffer[chIndex - 1] || 'B' == chBuffer[chIndex - 1])) Binary(chBuffer, chIndex - 1);
|
||||
else Decimal(chBuffer, chIndex);
|
||||
return true;
|
||||
}
|
||||
public void Exponent(int[] chBuffer, int chIndex)
|
||||
{
|
||||
chBuffer[chIndex++] = character;
|
||||
ReadCh();
|
||||
while (0xFFFF != character && (character == 43 || character == 45 || IsDigit(character)))
|
||||
{
|
||||
if (chIndex >= chBuffer.Length) return;
|
||||
chBuffer[chIndex++] = character;
|
||||
ReadCh();
|
||||
}
|
||||
Decimal(chBuffer, chIndex);
|
||||
}
|
||||
public void Binary(int[] chBuffer,int chIndex)
|
||||
{
|
||||
int value=0;
|
||||
int multiplier=1;
|
||||
|
||||
for(--chIndex;chIndex>=0;chIndex--)
|
||||
{
|
||||
switch(chBuffer[chIndex])
|
||||
{
|
||||
case '0' : break;
|
||||
case '1' : {value+=multiplier;break;}
|
||||
default : {Emit(ScanSymbols.unknown1);return;}
|
||||
}
|
||||
multiplier*=2;
|
||||
}
|
||||
Emit(ScanSymbols.numeral1,value);
|
||||
}
|
||||
public void Decimal(int[] chBuffer,int chIndex)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int index = 0; index < chBuffer.Length; index++) sb.Append((char)chBuffer[index]);
|
||||
double value = double.Parse(sb.ToString());
|
||||
Emit(ScanSymbols.numeral1,value);
|
||||
}
|
||||
public bool IsInHex(int character)
|
||||
{
|
||||
if ('A'.Equals(character) || 'B'.Equals(character) || 'C'.Equals(character) || 'D'.Equals(character) || 'E'.Equals(character) || 'F'.Equals(character) ||
|
||||
'a'.Equals(character) || 'b'.Equals(character) || 'c'.Equals(character) || 'd'.Equals(character) || 'e'.Equals(character) || 'f'.Equals(character)) return true;
|
||||
return false;
|
||||
}
|
||||
void Hex(int[] chBuffer, int chIndex)
|
||||
{
|
||||
int value=0;
|
||||
int multiplier=1;
|
||||
|
||||
for(--chIndex;chIndex>=0;chIndex--)
|
||||
{
|
||||
switch(chBuffer[chIndex])
|
||||
{
|
||||
case '0' : break;
|
||||
case '1' : {value+=multiplier;break;}
|
||||
case '2' : {value+=multiplier*2;break;}
|
||||
case '3' : {value+=multiplier*3;break;}
|
||||
case '4' : {value+=multiplier*4;break;}
|
||||
case '5' : {value+=multiplier*5;break;}
|
||||
case '6' : {value+=multiplier*6;break;}
|
||||
case '7' : {value+=multiplier*7;break;}
|
||||
case '8' : {value+=multiplier*8;break;}
|
||||
case '9' : {value+=multiplier*9;break;}
|
||||
case 'a' :
|
||||
case 'A' : {value+=multiplier*10;break;}
|
||||
case 'b' :
|
||||
case 'B' : {value+=multiplier*11;break;}
|
||||
case 'c' :
|
||||
case 'C' : {value+=multiplier*12;break;}
|
||||
case 'd' :
|
||||
case 'D' : {value+=multiplier*13;break;}
|
||||
case 'e' :
|
||||
case 'E' : {value+=multiplier*14;break;}
|
||||
case 'f' :
|
||||
case 'F' : {value+=multiplier*15;break;}
|
||||
default : {Emit(ScanSymbols.unknown1);return;}
|
||||
}
|
||||
multiplier*=16;
|
||||
}
|
||||
Emit(ScanSymbols.numeral1,value);
|
||||
}
|
||||
|
||||
public void SkipSeparators()
|
||||
{
|
||||
while (character.Equals((int)WhiteSpace.spacechar) || character.Equals((int)WhiteSpace.tabchar)) ReadCh();
|
||||
}
|
||||
public void ScanNewLine()
|
||||
{
|
||||
Emit(ScanSymbols.newline1);
|
||||
ReadCh();
|
||||
while (character.Equals('\n') || character.Equals('\n')) ReadCh();
|
||||
}
|
||||
public void ScanAnd()
|
||||
{
|
||||
int character=0;
|
||||
|
||||
Peek(ref character);
|
||||
if ('&'.Equals((char)character))
|
||||
{
|
||||
Emit(ScanSymbols.andand1);
|
||||
ReadCh();
|
||||
ReadCh();
|
||||
}
|
||||
else
|
||||
{
|
||||
Emit(ScanSymbols.unknown1);
|
||||
ReadCh();
|
||||
}
|
||||
}
|
||||
public void ScanOr()
|
||||
{
|
||||
int character=0;
|
||||
|
||||
Peek(ref character);
|
||||
if ('|'.Equals((char)character))
|
||||
{
|
||||
Emit(ScanSymbols.oror1);
|
||||
ReadCh();
|
||||
ReadCh();
|
||||
}
|
||||
else
|
||||
{
|
||||
Emit(ScanSymbols.unknown1);
|
||||
ReadCh();
|
||||
}
|
||||
}
|
||||
public void ScanEqual()
|
||||
{
|
||||
int character=0;
|
||||
|
||||
Peek(ref character);
|
||||
if ('='.Equals((char)character))
|
||||
{
|
||||
Emit(ScanSymbols.equalequal1);
|
||||
ReadCh();
|
||||
ReadCh();
|
||||
}
|
||||
else
|
||||
{
|
||||
Emit(ScanSymbols.equal1);
|
||||
ReadCh();
|
||||
}
|
||||
}
|
||||
public void ScanLess()
|
||||
{
|
||||
int character=0;
|
||||
|
||||
Peek(ref character);
|
||||
if ('='.Equals((char)character))
|
||||
{
|
||||
Emit(ScanSymbols.lessequal1);
|
||||
ReadCh();
|
||||
ReadCh();
|
||||
}
|
||||
else if ('>'.Equals((char)character))
|
||||
{
|
||||
Emit(ScanSymbols.notequal1);
|
||||
ReadCh();
|
||||
ReadCh();
|
||||
}
|
||||
else
|
||||
{
|
||||
Emit(ScanSymbols.less1);
|
||||
ReadCh();
|
||||
}
|
||||
}
|
||||
public void ScanGreater()
|
||||
{
|
||||
int character=0;
|
||||
|
||||
Peek(ref character);
|
||||
if ('='.Equals((char)character))
|
||||
{
|
||||
Emit(ScanSymbols.greaterequal1);
|
||||
ReadCh();
|
||||
ReadCh();
|
||||
}
|
||||
else
|
||||
{
|
||||
Emit(ScanSymbols.greater1);
|
||||
ReadCh();
|
||||
}
|
||||
}
|
||||
public void ScanComment()
|
||||
{
|
||||
ReadCh();
|
||||
while(!character.Equals('\r')&&!character.Equals(0xFFFF))ReadCh();
|
||||
}
|
||||
public bool IsAlpha(int character)
|
||||
{
|
||||
return Char.IsLetter((char)character);
|
||||
}
|
||||
public bool IsDigit(int character)
|
||||
{
|
||||
return Char.IsDigit((char)character);
|
||||
}
|
||||
public bool IsKeySymbol()
|
||||
{
|
||||
return ('}'==character||'{'==character||'<'==character||'>'==character||';'==character || ' ' == character || ',' == character || '[' == character || ']' == character || ':' == character || '*' == character || '/' == character || '+' == character || '-' == character || '(' == character || ')' == character ||'='==character);
|
||||
}
|
||||
public void ScanLiteral()
|
||||
{
|
||||
int character;
|
||||
word = new StringBuilder();
|
||||
ReadCh();
|
||||
character = this.character;
|
||||
if (character == '\'')
|
||||
{
|
||||
Emit(ScanSymbols.literal1,word.ToString());
|
||||
ReadCh();
|
||||
return;
|
||||
}
|
||||
while (-1!=character && 0xFFFF != this.character && 0x0D != this.character)
|
||||
{
|
||||
if ('\'' == this.character)
|
||||
{
|
||||
char peekChar=(char)0;
|
||||
PeekCh(ref peekChar);
|
||||
if ('\'' != peekChar) break;
|
||||
ReadCh();
|
||||
}
|
||||
word.Append((char)this.character);
|
||||
if (!ReadCh()) break;
|
||||
}
|
||||
ReadCh();
|
||||
Emit(ScanSymbols.literal1,word.ToString());
|
||||
}
|
||||
public void ScanUnknown()
|
||||
{
|
||||
ReadCh();
|
||||
Emit(ScanSymbols.unknown1);
|
||||
}
|
||||
public static String SymbolToLiteralString(Scanner.ScanSymbols code)
|
||||
{
|
||||
switch(code)
|
||||
{
|
||||
case Scanner.ScanSymbols.directive_clear_modified1 :
|
||||
return "directive_clear_modified1";
|
||||
case Scanner.ScanSymbols.declare1 :
|
||||
return "declare";
|
||||
case Scanner.ScanSymbols.unknown1 :
|
||||
return "unknown symbol";
|
||||
case Scanner.ScanSymbols.assign1 :
|
||||
return "assignment";
|
||||
case Scanner.ScanSymbols.if1 :
|
||||
return "if";
|
||||
case Scanner.ScanSymbols.while1 :
|
||||
return "while";
|
||||
case Scanner.ScanSymbols.then1 :
|
||||
return "then";
|
||||
case Scanner.ScanSymbols.else1 :
|
||||
return "else";
|
||||
case Scanner.ScanSymbols.goto1 :
|
||||
return "goto";
|
||||
case Scanner.ScanSymbols.equal1 :
|
||||
return "=";
|
||||
case Scanner.ScanSymbols.equalequal1 :
|
||||
return "==";
|
||||
case Scanner.ScanSymbols.less1 :
|
||||
return "<";
|
||||
case Scanner.ScanSymbols.lessequal1 :
|
||||
return "<=";
|
||||
case Scanner.ScanSymbols.greater1 :
|
||||
return ">";
|
||||
case Scanner.ScanSymbols.greaterequal1 :
|
||||
return ">=";
|
||||
case Scanner.ScanSymbols.notequal1 :
|
||||
return "<>";
|
||||
case Scanner.ScanSymbols.variable1 :
|
||||
return "variable";
|
||||
case Scanner.ScanSymbols.asterisk1 :
|
||||
return "*";
|
||||
case Scanner.ScanSymbols.apostrophe1 :
|
||||
return "'";
|
||||
case Scanner.ScanSymbols.comma1 :
|
||||
return ",";
|
||||
case Scanner.ScanSymbols.label1 :
|
||||
return "label";
|
||||
case Scanner.ScanSymbols.literal1 :
|
||||
return "'";
|
||||
case Scanner.ScanSymbols.leftcurly1 :
|
||||
return "{";
|
||||
case Scanner.ScanSymbols.rightcurly1 :
|
||||
return "}";
|
||||
case Scanner.ScanSymbols.leftbracket1 :
|
||||
return "[";
|
||||
case Scanner.ScanSymbols.rightbracket1 :
|
||||
return "]";
|
||||
case Scanner.ScanSymbols.numeral1 :
|
||||
return "numeral";
|
||||
case Scanner.ScanSymbols.char1 :
|
||||
return "character";
|
||||
case Scanner.ScanSymbols.divide1 :
|
||||
return "/";
|
||||
case Scanner.ScanSymbols.plus1 :
|
||||
return "+";
|
||||
case Scanner.ScanSymbols.minus1 :
|
||||
return "-";
|
||||
case Scanner.ScanSymbols.leftparen1 :
|
||||
return "(";
|
||||
case Scanner.ScanSymbols.rightparen1 :
|
||||
return ")";
|
||||
case Scanner.ScanSymbols.newline1 :
|
||||
return "newline";
|
||||
case Scanner.ScanSymbols.semicolon1 :
|
||||
return ";";
|
||||
case Scanner.ScanSymbols.endtext1 :
|
||||
return ";";
|
||||
case Scanner.ScanSymbols.end1 :
|
||||
return "end";
|
||||
case Scanner.ScanSymbols.andand1 :
|
||||
return "&&";
|
||||
case Scanner.ScanSymbols.oror1 :
|
||||
return "||";
|
||||
case Scanner.ScanSymbols.abs1 :
|
||||
return "abs";
|
||||
case Scanner.ScanSymbols.pow1 :
|
||||
return "pow";
|
||||
case Scanner.ScanSymbols.sqrt1 :
|
||||
return "sqrt";
|
||||
case Scanner.ScanSymbols.not1 :
|
||||
return "!";
|
||||
case Scanner.ScanSymbols.convert1 :
|
||||
return "convert";
|
||||
case Scanner.ScanSymbols.in1 :
|
||||
return "in";
|
||||
case Scanner.ScanSymbols.like1 :
|
||||
return "like";
|
||||
case Scanner.ScanSymbols.trim1 :
|
||||
return "trim";
|
||||
case Scanner.ScanSymbols.upper1 :
|
||||
return "upper";
|
||||
case Scanner.ScanSymbols.lower1 :
|
||||
return "lower";
|
||||
case Scanner.ScanSymbols.substring1 :
|
||||
return "substring";
|
||||
case Scanner.ScanSymbols.getprice1 :
|
||||
return "getprice";
|
||||
case Scanner.ScanSymbols.null1 :
|
||||
return "null";
|
||||
case Scanner.ScanSymbols.isnull1 :
|
||||
return "isnull";
|
||||
default :
|
||||
return "";
|
||||
}
|
||||
}
|
||||
public static String SymbolToString(Scanner.ScanSymbols code)
|
||||
{
|
||||
switch(code)
|
||||
{
|
||||
case Scanner.ScanSymbols.directive_clear_modified1 :
|
||||
return "directive_clear_modified1";
|
||||
case Scanner.ScanSymbols.declare1 :
|
||||
return "declare1";
|
||||
case Scanner.ScanSymbols.unknown1 :
|
||||
return "unknown1";
|
||||
case Scanner.ScanSymbols.assign1 :
|
||||
return "assign1";
|
||||
case Scanner.ScanSymbols.if1 :
|
||||
return "if1";
|
||||
case Scanner.ScanSymbols.while1 :
|
||||
return "while1";
|
||||
case Scanner.ScanSymbols.then1 :
|
||||
return "then1";
|
||||
case Scanner.ScanSymbols.else1 :
|
||||
return "else1";
|
||||
case Scanner.ScanSymbols.goto1 :
|
||||
return "goto1";
|
||||
case Scanner.ScanSymbols.equal1 :
|
||||
return "equal1";
|
||||
case Scanner.ScanSymbols.equalequal1 :
|
||||
return "equalequal1";
|
||||
case Scanner.ScanSymbols.less1 :
|
||||
return "less1";
|
||||
case Scanner.ScanSymbols.lessequal1 :
|
||||
return "lessequal1";
|
||||
case Scanner.ScanSymbols.greater1 :
|
||||
return "greater1";
|
||||
case Scanner.ScanSymbols.greaterequal1 :
|
||||
return "greaterequal1";
|
||||
case Scanner.ScanSymbols.notequal1 :
|
||||
return "notequal1";
|
||||
case Scanner.ScanSymbols.variable1 :
|
||||
return "variable1";
|
||||
case Scanner.ScanSymbols.asterisk1 :
|
||||
return "asterisk1";
|
||||
case Scanner.ScanSymbols.apostrophe1 :
|
||||
return "apostrophe1";
|
||||
case Scanner.ScanSymbols.comma1 :
|
||||
return "comma1";
|
||||
case Scanner.ScanSymbols.label1 :
|
||||
return "label1";
|
||||
case Scanner.ScanSymbols.literal1 :
|
||||
return "literal1";
|
||||
case Scanner.ScanSymbols.leftcurly1 :
|
||||
return "leftcurly1";
|
||||
case Scanner.ScanSymbols.rightcurly1 :
|
||||
return "rightcurly1";
|
||||
case Scanner.ScanSymbols.leftbracket1 :
|
||||
return "leftbracket1";
|
||||
case Scanner.ScanSymbols.rightbracket1 :
|
||||
return "rightbracket1";
|
||||
case Scanner.ScanSymbols.numeral1 :
|
||||
return "numeral1";
|
||||
case Scanner.ScanSymbols.char1 :
|
||||
return "char1";
|
||||
case Scanner.ScanSymbols.divide1 :
|
||||
return "divide1";
|
||||
case Scanner.ScanSymbols.plus1 :
|
||||
return "plus1";
|
||||
case Scanner.ScanSymbols.minus1 :
|
||||
return "minus1";
|
||||
case Scanner.ScanSymbols.leftparen1 :
|
||||
return "leftparen1";
|
||||
case Scanner.ScanSymbols.rightparen1 :
|
||||
return "rightparen1";
|
||||
case Scanner.ScanSymbols.newline1 :
|
||||
return "newline1";
|
||||
case Scanner.ScanSymbols.semicolon1 :
|
||||
return "semicolon1";
|
||||
case Scanner.ScanSymbols.endtext1 :
|
||||
return "endtext1";
|
||||
case Scanner.ScanSymbols.end1 :
|
||||
return "end1";
|
||||
case Scanner.ScanSymbols.andand1 :
|
||||
return "andand1";
|
||||
case Scanner.ScanSymbols.oror1 :
|
||||
return "oror1";
|
||||
case Scanner.ScanSymbols.abs1 :
|
||||
return "abs1";
|
||||
case Scanner.ScanSymbols.pow1 :
|
||||
return "pow1";
|
||||
case Scanner.ScanSymbols.sqrt1 :
|
||||
return "sqrt1";
|
||||
case Scanner.ScanSymbols.not1 :
|
||||
return "not1";
|
||||
case Scanner.ScanSymbols.convert1 :
|
||||
return "convert1";
|
||||
case Scanner.ScanSymbols.in1 :
|
||||
return "in1";
|
||||
case Scanner.ScanSymbols.like1 :
|
||||
return "like1";
|
||||
case Scanner.ScanSymbols.trim1 :
|
||||
return "trim1";
|
||||
case Scanner.ScanSymbols.upper1 :
|
||||
return "upper1";
|
||||
case Scanner.ScanSymbols.lower1 :
|
||||
return "lower1";
|
||||
case Scanner.ScanSymbols.substring1 :
|
||||
return "substring1";
|
||||
case Scanner.ScanSymbols.getprice1 :
|
||||
return "getprice1";
|
||||
case Scanner.ScanSymbols.null1 :
|
||||
return "null1";
|
||||
case Scanner.ScanSymbols.isnull1 :
|
||||
return "isnull1";
|
||||
default :
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Axiom/Axiom.Core/Interpreter/StackElement.cs
Executable file
31
Axiom/Axiom.Core/Interpreter/StackElement.cs
Executable file
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
// FileName : StackElement.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class CodeStack
|
||||
{
|
||||
private Stack<StackElement> codeStack = new Stack<StackElement>();
|
||||
public CodeStack()
|
||||
{
|
||||
}
|
||||
public void Push(StackElement stackElement)
|
||||
{
|
||||
codeStack.Push(stackElement);
|
||||
}
|
||||
public StackElement Pop()
|
||||
{
|
||||
return codeStack.Pop();
|
||||
}
|
||||
}
|
||||
public class StackElement
|
||||
{
|
||||
public StackElement()
|
||||
{
|
||||
}
|
||||
public GenericData Value { get; set; }
|
||||
public Symbol Symbol { get; set; }
|
||||
}
|
||||
}
|
||||
40
Axiom/Axiom.Core/Interpreter/Symbol.cs
Executable file
40
Axiom/Axiom.Core/Interpreter/Symbol.cs
Executable file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
// FileName : Symbol.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class Symbol
|
||||
{
|
||||
public enum SymbolType { KeywordSymbol,FunctionSymbol, UserSymbol, UserDynamicSymbol, DirectiveSymbol};
|
||||
private bool isMutable=true;
|
||||
private bool isModified=false;
|
||||
public Symbol()
|
||||
{
|
||||
IsMutable = true;
|
||||
}
|
||||
public Symbol(String symbolName, Scanner.ScanSymbols identifier,SymbolType symbolType = SymbolType.UserSymbol)
|
||||
{
|
||||
this.SymbolName = symbolName;
|
||||
this.Identifier = identifier;
|
||||
this.TypeOfSymbol = symbolType;
|
||||
if (symbolType.Equals(SymbolType.UserSymbol)) IsMutable = true;
|
||||
else IsMutable = false;
|
||||
}
|
||||
public String SymbolName{get;set;}
|
||||
public Scanner.ScanSymbols Identifier{get;set;}
|
||||
public SymbolType TypeOfSymbol{get;set;}
|
||||
public GenericData GenericData { get; set; }
|
||||
public bool IsMutable
|
||||
{
|
||||
get { return isMutable; }
|
||||
set {isMutable=value;}
|
||||
}
|
||||
public bool IsModified
|
||||
{
|
||||
get {return isModified;}
|
||||
set {isModified=value;}
|
||||
}
|
||||
}
|
||||
}
|
||||
205
Axiom/Axiom.Core/Interpreter/SymbolTable.cs
Executable file
205
Axiom/Axiom.Core/Interpreter/SymbolTable.cs
Executable file
@@ -0,0 +1,205 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Data;
|
||||
using log4net;
|
||||
using System.Reflection;
|
||||
using Axiom.Utils;
|
||||
|
||||
// FileName : SymbolTable.cs
|
||||
// Author : Sean Kessler
|
||||
|
||||
namespace Axiom.Interpreter
|
||||
{
|
||||
public class SymbolTable : Dictionary<String, Symbol>
|
||||
{
|
||||
private static ILog logger = LogManager.GetLogger(typeof(SymbolTable));
|
||||
private Dictionary<int, Symbol> symbolsById = new Dictionary<int, Symbol>();
|
||||
public SymbolTable()
|
||||
{
|
||||
CreateSymbolTable();
|
||||
}
|
||||
// Example: symbolTable.AddObjects(new Object[]{position}.ToList());
|
||||
public void AddObjects(List<Object> objects)
|
||||
{
|
||||
foreach(Object obj in objects)
|
||||
{
|
||||
AddObject(obj);
|
||||
}
|
||||
}
|
||||
// Example: symbolTable.AddObject("Num",55.00);
|
||||
public void AddObject(String symbolName,Object obj)
|
||||
{
|
||||
Type objectType=obj.GetType();
|
||||
Symbol symbol=new Symbol(symbolName,Scanner.ScanSymbols.variable1,Symbol.SymbolType.UserSymbol);
|
||||
Add(symbolName,symbol);
|
||||
symbol.GenericData = new GenericData();
|
||||
String stringRepData = obj.ToString();
|
||||
symbol.GenericData.Data = GenericData.ConvertType(objectType, stringRepData);
|
||||
}
|
||||
// Example: symbolTable.AddObject(position)
|
||||
public void AddObject(Object obj)
|
||||
{
|
||||
DataTable dataTable=new DataTable();
|
||||
Type classType=obj.GetType();
|
||||
PropertyInfo[] properties = classType.GetProperties();
|
||||
foreach(PropertyInfo propertyInfo in properties)
|
||||
{
|
||||
MethodInfo methodInfo=propertyInfo.GetGetMethod();
|
||||
String methodName=methodInfo.Name;
|
||||
if(!methodName.StartsWith("get_"))continue;
|
||||
methodName=methodName.Replace("get_",null);
|
||||
DataColumn dataColumn=new DataColumn();
|
||||
dataColumn.ColumnName=methodName;
|
||||
dataColumn.DataType=methodInfo.ReturnType; // capture the data type
|
||||
dataTable.Columns.Add(dataColumn);
|
||||
}
|
||||
DataRow dataRow=dataTable.NewRow();
|
||||
foreach(PropertyInfo propertyInfo in properties)
|
||||
{
|
||||
MethodInfo methodInfo=propertyInfo.GetGetMethod();
|
||||
String methodName=methodInfo.Name;
|
||||
if(!methodName.StartsWith("get_"))continue;
|
||||
methodName=methodName.Replace("get_",null);
|
||||
dataRow[methodName]=methodInfo.Invoke(obj,null);
|
||||
}
|
||||
AddUserSymbols(dataTable);
|
||||
AddUserValues(dataRow,dataTable);
|
||||
}
|
||||
public void AddUserSymbols(DataTable dataTable)
|
||||
{
|
||||
DataColumnCollection columns=dataTable.Columns;
|
||||
foreach (DataColumn column in columns)
|
||||
{
|
||||
String symbolName = column.ColumnName;
|
||||
if(ContainsKey(symbolName))continue;
|
||||
Symbol symbol=new Symbol(symbolName,Scanner.ScanSymbols.variable1,Symbol.SymbolType.UserSymbol);
|
||||
Add(symbolName,symbol);
|
||||
}
|
||||
}
|
||||
public void AddUserValues(DataRow dataRow,DataTable dataTable)
|
||||
{
|
||||
DataColumnCollection columns = dataTable.Columns;
|
||||
for(int index=0;index<columns.Count;index++)
|
||||
{
|
||||
DataColumn column = columns[index];
|
||||
String symbolName = column.ColumnName;
|
||||
if(!ContainsKey(symbolName))continue;
|
||||
Symbol symbol = this[symbolName];
|
||||
symbol.GenericData = new GenericData();
|
||||
String stringRepData = dataRow[index].ToString();
|
||||
symbol.GenericData.Data = GenericData.ConvertType(column.DataType, stringRepData);
|
||||
}
|
||||
}
|
||||
public List<Symbol> GetModfied()
|
||||
{
|
||||
List<Symbol> symbols = this.Values.ToList();
|
||||
symbols = (from Symbol symbol in symbols where symbol.TypeOfSymbol.Equals(Symbol.SymbolType.UserSymbol) && symbol.IsModified select symbol).ToList();
|
||||
return symbols;
|
||||
}
|
||||
public String GetModifiedSymbolNames()
|
||||
{
|
||||
List<Symbol> symbols = this.Values.ToList();
|
||||
List<String> symbolNames = (from Symbol symbol in symbols where symbol.TypeOfSymbol.Equals(Symbol.SymbolType.UserSymbol) && symbol.IsModified select symbol.SymbolName).ToList();
|
||||
return Utility.ListToString(symbolNames);
|
||||
}
|
||||
public void Reset()
|
||||
{
|
||||
ClearModified();
|
||||
ClearData();
|
||||
RemoveUserDynamicSymbols();
|
||||
}
|
||||
public void RemoveUserDynamicSymbols()
|
||||
{
|
||||
List<Symbol> symbols = this.Values.ToList();
|
||||
symbols = (from Symbol symbol in symbols where symbol.TypeOfSymbol.Equals(Symbol.SymbolType.UserDynamicSymbol) select symbol).ToList();
|
||||
foreach (Symbol symbol in symbols) this.Remove(symbol.SymbolName);
|
||||
}
|
||||
public void ClearModified()
|
||||
{
|
||||
List<Symbol> symbols = this.Values.ToList();
|
||||
symbols = (from Symbol symbol in symbols where symbol.TypeOfSymbol.Equals(Symbol.SymbolType.UserSymbol) select symbol).ToList();
|
||||
foreach (Symbol symbol in symbols) symbol.IsModified = false;
|
||||
}
|
||||
public void ClearData()
|
||||
{
|
||||
List<Symbol> symbols = this.Values.ToList();
|
||||
foreach (Symbol symbol in symbols) symbol.GenericData = null;
|
||||
}
|
||||
public bool IsModified()
|
||||
{
|
||||
List<Symbol> symbols = this.Values.ToList();
|
||||
symbols = (from Symbol symbol in symbols where symbol.TypeOfSymbol.Equals(Symbol.SymbolType.UserSymbol) select symbol).ToList();
|
||||
foreach (Symbol symbol in symbols) if(symbol.IsModified)return true;
|
||||
return false;
|
||||
}
|
||||
private void CreateSymbolTable()
|
||||
{
|
||||
Add("#directive_clear_modified",new Symbol("#directive_clear_modified",Scanner.ScanSymbols.directive_clear_modified1,Symbol.SymbolType.DirectiveSymbol));
|
||||
Add("declare",new Symbol("declare",Scanner.ScanSymbols.declare1,Symbol.SymbolType.KeywordSymbol));
|
||||
Add("if",new Symbol("if",Scanner.ScanSymbols.if1,Symbol.SymbolType.KeywordSymbol));
|
||||
Add("break",new Symbol("break",Scanner.ScanSymbols.break1,Symbol.SymbolType.KeywordSymbol));
|
||||
Add("while",new Symbol("while",Scanner.ScanSymbols.while1,Symbol.SymbolType.KeywordSymbol));
|
||||
Add("then", new Symbol("then", Scanner.ScanSymbols.then1, Symbol.SymbolType.KeywordSymbol));
|
||||
Add("else", new Symbol("else", Scanner.ScanSymbols.else1, Symbol.SymbolType.KeywordSymbol));
|
||||
Add("and", new Symbol("and", Scanner.ScanSymbols.andand1, Symbol.SymbolType.KeywordSymbol));
|
||||
Add("or", new Symbol("or", Scanner.ScanSymbols.oror1, Symbol.SymbolType.KeywordSymbol));
|
||||
Add("null", new Symbol("null", Scanner.ScanSymbols.null1, Symbol.SymbolType.KeywordSymbol));
|
||||
// high level functions
|
||||
Symbol symbol=null;
|
||||
|
||||
symbol=new Symbol("abs", Scanner.ScanSymbols.abs1, Symbol.SymbolType.FunctionSymbol); // abs(x)
|
||||
Add("abs",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.abs1,symbol);
|
||||
|
||||
symbol=new Symbol("pow", Scanner.ScanSymbols.pow1, Symbol.SymbolType.FunctionSymbol); // pow(x,y)
|
||||
Add("pow",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.pow1,symbol);
|
||||
|
||||
symbol=new Symbol("sqrt", Scanner.ScanSymbols.sqrt1, Symbol.SymbolType.FunctionSymbol); // sqrt(x)
|
||||
Add("sqrt",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.sqrt1,symbol);
|
||||
|
||||
symbol=new Symbol("convert", Scanner.ScanSymbols.convert1, Symbol.SymbolType.FunctionSymbol); // convert(x,'{type}') convert(variable|literal,'{datatype}') convert('01-02-2018','System.DateTime')
|
||||
Add("convert",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.convert1,symbol);
|
||||
|
||||
symbol=new Symbol("substring", Scanner.ScanSymbols.substring1, Symbol.SymbolType.FunctionSymbol); // substring(variable|literal,numeral|variable,numeral|variable)
|
||||
Add("substring",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.substring1,symbol);
|
||||
|
||||
symbol=new Symbol("getprice", Scanner.ScanSymbols.getprice1, Symbol.SymbolType.FunctionSymbol); // getprice(variable|literal,variable|literal) getprice('midd','07-12-2021')
|
||||
Add("getprice",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.getprice1,symbol);
|
||||
|
||||
symbol=new Symbol("in", Scanner.ScanSymbols.in1, Symbol.SymbolType.FunctionSymbol); // in('X','Y','Z')
|
||||
Add("in",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.in1,symbol);
|
||||
|
||||
symbol=new Symbol("like", Scanner.ScanSymbols.like1, Symbol.SymbolType.FunctionSymbol); // like 'x%'
|
||||
Add("like",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.like1,symbol);
|
||||
|
||||
symbol=new Symbol("trim", Scanner.ScanSymbols.trim1, Symbol.SymbolType.FunctionSymbol); // trim('x')
|
||||
Add("trim",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.trim1,symbol);
|
||||
|
||||
symbol=new Symbol("upper", Scanner.ScanSymbols.upper1, Symbol.SymbolType.FunctionSymbol); // upper('x')
|
||||
Add("upper",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.upper1,symbol);
|
||||
|
||||
symbol=new Symbol("lower", Scanner.ScanSymbols.lower1, Symbol.SymbolType.FunctionSymbol); // lower('x')
|
||||
Add("lower",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.lower1,symbol);
|
||||
|
||||
symbol=new Symbol("isnull", Scanner.ScanSymbols.isnull1, Symbol.SymbolType.FunctionSymbol); // isnull(variable)
|
||||
Add("isnull",symbol);
|
||||
symbolsById.Add((int)Scanner.ScanSymbols.isnull1,symbol);
|
||||
}
|
||||
public Symbol Find(Scanner.ScanSymbols identifier)
|
||||
{
|
||||
if (!symbolsById.ContainsKey((int)identifier)) return null;
|
||||
return this[symbolsById[(int)identifier].SymbolName];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user