Skip to content

Commit

Permalink
Added alternative-access, fixed #26
Browse files Browse the repository at this point in the history
  • Loading branch information
Azer0s committed Aug 21, 2017
1 parent 10fd1df commit 2154996
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 141 deletions.
5 changes: 3 additions & 2 deletions HadesLang/fib.hades
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
c as num closed = fib:[1,1]
out:c
c as word closed = 'Hello world'
fib:[1,1]
func fib[num a, num b]
out:c
asLongAs[true]
out:a
out:b
Expand Down
108 changes: 54 additions & 54 deletions Interpreter/Evaluator.cs

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions Interpreter/FileInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public FileInterpreter(string path)
LoadFunctions();
}

public string Execute(Interpreter interpreter, int start = 0, int end = -1)
public string Execute(Interpreter interpreter, string access, int start = 0, int end = -1)
{
var output = interpreter.Output;
interpreter.Output = new NoOutput();
Expand All @@ -69,9 +69,9 @@ public string Execute(Interpreter interpreter, int start = 0, int end = -1)
var groups = RegexCollection.Store.Case.Match(Lines[i]).Groups.OfType<Group>().Select(a => a.Value)
.ToArray();

if (bool.Parse(interpreter.InterpretLine(groups[1],FAccess,this)))
if (bool.Parse(interpreter.InterpretLine(groups[1],access,this,FAccess)))
{
var result = Execute(interpreter, block.start + 1, block.end);
var result = Execute(interpreter,access: access, start: block.start + 1, end: block.end);

if (!IsNullOrEmpty(result))
{
Expand All @@ -91,9 +91,9 @@ public string Execute(Interpreter interpreter, int start = 0, int end = -1)
var groups = RegexCollection.Store.AsLongAs.Match(Lines[i]).Groups.OfType<Group>().Select(a => a.Value)
.ToArray();

while (bool.Parse(interpreter.InterpretLine(groups[1], FAccess,this)))
while (bool.Parse(interpreter.InterpretLine(groups[1], access,this,FAccess)))
{
var result = Execute(interpreter, block.start + 1, block.end);
var result = Execute(interpreter, start: block.start + 1, end: block.end,access:access);

if (!IsNullOrEmpty(result))
{
Expand All @@ -112,10 +112,10 @@ public string Execute(Interpreter interpreter, int start = 0, int end = -1)
if (RegexCollection.Store.Put.IsMatch(Lines[i]))
{
interpreter.Output = output;
return interpreter.InterpretLine(RegexCollection.Store.Put.Match(Lines[i]).Groups[1].Value,FAccess,this);
return interpreter.InterpretLine(RegexCollection.Store.Put.Match(Lines[i]).Groups[1].Value,access,this,FAccess);
}

var interresult = interpreter.InterpretLine(Lines[i], FAccess, this);
var interresult = interpreter.InterpretLine(Lines[i], access, this,FAccess);
//Function call
if (RegexCollection.Store.Function.IsMatch(Lines[i]) && Functions.Any(a => a.Name == RegexCollection.Store.Function.Match(Lines[i]).Groups[1].Value))
{
Expand Down Expand Up @@ -149,15 +149,12 @@ public string CallFunction(string function,Interpreter interpreter)

for (var i = 0; i < args.Count; i++)
{
interpreter.Evaluator.CreateVariable($"{expectedArgs[i].Key} as {expectedArgs[i].Value.ToString().ToLower()} closed = {args[i]}",FAccess,interpreter,this);
interpreter.Evaluator.CreateVariable($"{expectedArgs[i].Key} as {expectedArgs[i].Value.ToString().ToLower()} closed = {args[i]}",$"{FAccess}@{groups[1]}",interpreter,this,FAccess);
}

var result = Execute(interpreter, func.Postition.Item1+1, func.Postition.Item2);
var result = Execute(interpreter, start: func.Postition.Item1+1, end: func.Postition.Item2,access: $"{FAccess}@{groups[1]}");

foreach (var keyValuePair in expectedArgs)
{
interpreter.Evaluator.Unload(keyValuePair.Key, FAccess);
}
interpreter.Evaluator.Unload("all", $"{FAccess}@{groups[1]}");

return result;
}
Expand Down
161 changes: 130 additions & 31 deletions Interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Interpreter
public readonly Evaluator Evaluator;


public Interpreter(IScriptOutput output,IScriptOutput explicitOutput)
public Interpreter(IScriptOutput output, IScriptOutput explicitOutput)
{
Output = output;
ExplicitOutput = explicitOutput;
Expand All @@ -44,7 +44,7 @@ public Interpreter(IScriptOutput output,IScriptOutput explicitOutput)
Cache.Instance.LoadFiles = new List<string>();
}

public string InterpretLine(string lineToInterprete, string access, FileInterpreter file)
public string InterpretLine(string lineToInterprete, string access, FileInterpreter file, string altAccess, string function = "", bool isolateVars = false)
{
if (IsNullOrEmpty(lineToInterprete))
{
Expand All @@ -54,7 +54,7 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
//Variable decleration
if (RegexCollection.Store.CreateVariable.IsMatch(lineToInterprete))
{
Output.WriteLine(Evaluator.CreateVariable(lineToInterprete, access,this,file));
Output.WriteLine(Evaluator.CreateVariable(lineToInterprete, access, this, file, altAccess));
return Empty;
}

Expand All @@ -63,11 +63,25 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
{
try
{
Output.WriteLine(Evaluator.CreateArray(lineToInterprete, access, this,file));
Output.WriteLine(Evaluator.CreateArray(lineToInterprete, access, this, file, altAccess));
}
catch (Exception e)
{
ExplicitOutput.WriteLine(e.Message);
try
{
if (!IsNullOrEmpty(altAccess))
{
Output.WriteLine(Evaluator.CreateArray(lineToInterprete, altAccess, this, file, altAccess));
}
else
{
throw e;
}
}
catch (Exception exception)
{
ExplicitOutput.WriteLine(e.Message);
}
}
return Empty;
}
Expand All @@ -77,11 +91,25 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
{
try
{
Output.WriteLine(Evaluator.AssignToArrayAtPos(lineToInterprete, access, this,file));
Output.WriteLine(Evaluator.AssignToArrayAtPos(lineToInterprete, access, this, file, altAccess));
}
catch (Exception e)
{
ExplicitOutput.WriteLine(e.Message);
try
{
if (!IsNullOrEmpty(altAccess))
{
Output.WriteLine(Evaluator.AssignToArrayAtPos(lineToInterprete, altAccess, this, file, altAccess));
}
else
{
throw e;
}
}
catch (Exception exception)
{
ExplicitOutput.WriteLine(e.Message);
}
}
return Empty;
}
Expand All @@ -91,11 +119,25 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
{
try
{
Output.WriteLine(Evaluator.AssignToVariable(lineToInterprete, access, true, this,file));
Output.WriteLine(Evaluator.AssignToVariable(lineToInterprete, access, true, this, file, altAccess));
}
catch (Exception e)
{
ExplicitOutput.WriteLine(e.Message);
try
{
if (!IsNullOrEmpty(altAccess))
{
Output.WriteLine(Evaluator.AssignToVariable(lineToInterprete, altAccess, true, this, file, altAccess));
}
else
{
throw e;
}
}
catch (Exception exception)
{
ExplicitOutput.WriteLine(e.Message);
}
}
return Empty;
}
Expand All @@ -110,11 +152,11 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
var eOutput = ExplicitOutput;
Output = new NoOutput();
ExplicitOutput = new NoOutput();
lineToInterprete = $"{groups[1]} = ${groups[1].TrimStart('$')} {groups[2]} {InterpretLine(groups[3],access,file)}";
lineToInterprete = $"{groups[1]} = ${groups[1].TrimStart('$')} {groups[2]} {InterpretLine(groups[3], access, file, altAccess)}";
Output = output;
ExplicitOutput = eOutput;

return InterpretLine(lineToInterprete, access,file);
return InterpretLine(lineToInterprete, access, file, altAccess);
}

//Method calls
Expand All @@ -133,7 +175,7 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
{
try
{
return file.CallFunction(lineToInterprete,this);
return file.CallFunction(lineToInterprete, this);
}
catch (Exception e)
{
Expand Down Expand Up @@ -161,12 +203,26 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
string result;
try
{
result = Evaluator.EvaluateOut(lineToInterprete, access, this,file).TrimStart('\'').TrimEnd('\'');
result = Evaluator.EvaluateOut(lineToInterprete, access, this, file, altAccess).TrimStart('\'').TrimEnd('\'');
}
catch (Exception e)
{
ExplicitOutput.WriteLine(e.Message);
return Empty;
try
{
if (!IsNullOrEmpty(altAccess))
{
result = Evaluator.EvaluateOut(lineToInterprete, altAccess, this, file, altAccess).TrimStart('\'').TrimEnd('\'');
}
else
{
throw e;
}
}
catch (Exception exception)
{
ExplicitOutput.WriteLine(e.Message);
return Empty;
}
}
ExplicitOutput.WriteLine(result);
return $"'{result}'";
Expand All @@ -175,7 +231,7 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
//Load
if (RegexCollection.Store.Load.IsMatch(lineToInterprete))
{
return Evaluator.LoadFile(lineToInterprete,this);
return Evaluator.LoadFile(lineToInterprete, this);
}

//Unload
Expand All @@ -187,7 +243,22 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
}
catch (Exception e)
{
ExplicitOutput.WriteLine(e.Message);
try
{
if (!IsNullOrEmpty(altAccess))
{
Output.WriteLine(Evaluator.Unload(RegexCollection.Store.Unload.Match(lineToInterprete).Groups[1].Value, altAccess));
}
else
{
throw e;
}
}
catch (Exception exception)
{
ExplicitOutput.WriteLine(e.Message);
return Empty;
}
}
return Empty;
}
Expand All @@ -197,7 +268,7 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
//Input
if (RegexCollection.Store.Input.IsMatch(lineToInterprete))
{
var result = Evaluator.Input(lineToInterprete, access, Output,this,file);
var result = Evaluator.Input(lineToInterprete, access, Output, this, file, altAccess);
Output.WriteLine(result.Message);
return result.Value;
}
Expand All @@ -216,12 +287,12 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
string result;
if (lineToInterprete.StartsWith("d"))
{
if (Evaluator.DataTypeFromData(lineToInterprete,true) == DataTypes.WORD)
if (Evaluator.DataTypeFromData(lineToInterprete, true) == DataTypes.WORD)
{
Output.WriteLine(DataTypes.WORD.ToString());
return DataTypes.WORD.ToString();
}
result = Evaluator.DataTypeFromData(InterpretLine(RegexCollection.Store.Type.Match(lineToInterprete).Groups[1].Value,access,file), true).ToString();
result = Evaluator.DataTypeFromData(InterpretLine(RegexCollection.Store.Type.Match(lineToInterprete).Groups[1].Value, access, file, altAccess), true).ToString();
}
else
{
Expand All @@ -237,7 +308,7 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
//Exists
if (RegexCollection.Store.Exists.IsMatch(lineToInterprete))
{
var result = Evaluator.Exists(RegexCollection.Store.Exists.Match(lineToInterprete).Groups[1].Value,access).Exists.ToString().ToLower();
var result = Evaluator.Exists(RegexCollection.Store.Exists.Match(lineToInterprete).Groups[1].Value, access).Exists.ToString().ToLower();
Output.WriteLine(result);
return result;
}
Expand Down Expand Up @@ -326,12 +397,26 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
var value = Empty;
try
{
value = Evaluator.GetArrayValue($"${lineToInterprete.TrimStart('$')}", access, this,file);
value = Evaluator.GetArrayValue($"${lineToInterprete.TrimStart('$')}", access, this, file, altAccess);
}
catch (Exception e)
{
ExplicitOutput.WriteLine(e.Message);
return value;
try
{
if (!IsNullOrEmpty(altAccess))
{
value = Evaluator.GetArrayValue($"${lineToInterprete.TrimStart('$')}", altAccess, this, file, altAccess);
}
else
{
throw e;
}
}
catch (Exception exception)
{
ExplicitOutput.WriteLine(e.Message);
return value;
}
}
Output.WriteLine(value.TrimStart('\'').TrimEnd('\''));
return value;
Expand All @@ -340,14 +425,14 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
//In/Decrease
if (RegexCollection.Store.InDeCrease.IsMatch(lineToInterprete))
{
Evaluator.InDeCrease(lineToInterprete, access, this,file);
Evaluator.InDeCrease(lineToInterprete, access, this, file, altAccess);
return Empty;
}

//Calculation
if ((lineToInterprete.ContainsFromList(Cache.Instance.CharList) || lineToInterprete.ContainsFromList(Cache.Instance.Replacement.Keys)) && !RegexCollection.Store.IsWord.IsMatch(lineToInterprete) && !lineToInterprete.StartsWith("#"))
{
var calculationResult = Evaluator.EvaluateCalculation(lineToInterprete.Replace("integ(","int(").Replace("%","#"), access, this,file);
var calculationResult = Evaluator.EvaluateCalculation(lineToInterprete.Replace("integ(", "int(").Replace("%", "#"), access, this, file, altAccess);
Output.WriteLine(calculationResult.Result);

if (calculationResult.Result != "NaN")
Expand Down Expand Up @@ -417,16 +502,30 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
}
catch (Exception e)
{
ExplicitOutput.WriteLine(e.Message);
return Empty;
try
{
if (!IsNullOrEmpty(altAccess))
{
variable = Evaluator.GetVariable(lineToInterprete.TrimStart('$'), altAccess);
}
else
{
throw e;
}
}
catch (Exception exception)
{
ExplicitOutput.WriteLine(e.Message);
return Empty;
}
}
if (variable is Variable)
{
var o = variable as Variable;
Output.WriteLine(o.Value);
return o.Value;
}
if(variable is Variables.Array)
if (variable is Variables.Array)
{
var o = variable as Variables.Array;
var result = o.Values.Aggregate("{", (current, keyValuePair) => current + $"{keyValuePair.Value},")
Expand All @@ -449,11 +548,11 @@ public string InterpretLine(string lineToInterprete, string access, FileInterpre
var eOutput = ExplicitOutput;
Output = new NoOutput();
ExplicitOutput = new NoOutput();
var valueToInterpret = InterpretLine(RegexCollection.Store.ForceThrough.Match(lineToInterprete).Groups[1].Value,access,file).TrimStart('\'').TrimEnd('\'');
var valueToInterpret = InterpretLine(RegexCollection.Store.ForceThrough.Match(lineToInterprete).Groups[1].Value, access, file, altAccess).TrimStart('\'').TrimEnd('\'');
Output = output;
ExplicitOutput = eOutput;

return InterpretLine(valueToInterpret, access,file);
return InterpretLine(valueToInterpret, access, file, altAccess);
}

return lineToInterprete;
Expand Down
Loading

0 comments on commit 2154996

Please sign in to comment.