Skip to content

Commit

Permalink
some refactor, also im fixed something (im forgot lol)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpGerg committed Feb 27, 2024
1 parent b3a7386 commit ee7a238
Show file tree
Hide file tree
Showing 57 changed files with 399 additions and 266 deletions.
Binary file removed Imports/UCA/Assembly-CSharp-firstpass.dll
Binary file not shown.
Binary file removed Imports/UCA/Assembly-CSharp.dll
Binary file not shown.
Binary file removed Imports/UCA/CommandSystem.Core.dll
Binary file not shown.
Binary file removed Imports/UCA/CustomHudAPI.dll
Binary file not shown.
Binary file removed Imports/UCA/Exiled.API.dll
Binary file not shown.
Binary file removed Imports/UCA/Exiled.Permissions.dll
Binary file not shown.
Binary file removed Imports/UCA/Exiled.Updater.dll
Binary file not shown.
Binary file removed Imports/UCA/Mirror.dll
Binary file not shown.
Binary file removed Imports/UCA/NorthwoodLib.dll
Binary file not shown.
Binary file removed Imports/UCA/SCPSLAudioApi.dll
Binary file not shown.
Binary file removed Imports/UCA/SemanticVersioning.dll
Binary file not shown.
Binary file not shown.
Binary file removed Imports/UCA/Unity.TextMeshPro.dll
Binary file not shown.
Binary file removed Imports/UCA/UnityEngine.CoreModule.dll
Binary file not shown.
Binary file removed Imports/UCA/UnityEngine.PhysicsModule.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed Imports/UCA/UnityEngine.TextRenderingModule.dll
Binary file not shown.
Binary file removed Imports/UCA/UnityEngine.UI.dll
Binary file not shown.
Binary file removed Imports/UCA/UnityEngine.UIElementsModule.dll
Binary file not shown.
Binary file removed Imports/UCA/UnityEngine.UIElementsNativeModule.dll
Binary file not shown.
Binary file removed Imports/UCA/UnityEngine.UIModule.dll
Binary file not shown.
20 changes: 1 addition & 19 deletions Paganism/Interpreter/Data/DataStorage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Paganism.Exceptions;
using Paganism.Interpreter.Data.Instances;
using Paganism.PParser.AST;
using System.Collections.Generic;

Expand All @@ -15,7 +14,7 @@ public abstract class DataStorage<T>

protected virtual IReadOnlyDictionary<string, T> Language { get; } = new Dictionary<string, T>();

public void Add(BlockStatementExpression expression, string name, T value)
public void Set(BlockStatementExpression expression, string name, T value)
{
if (expression is null)
{
Expand All @@ -38,23 +37,6 @@ public void Add(BlockStatementExpression expression, string name, T value)
}
}

public void Set(BlockStatementExpression expression, string name, T value)
{
if (expression is null)
{
GlobalDeclarated[name] = value;
return;
}

if (!Declarated.TryGetValue(expression, out _))
{
Declarated.Add(expression, new Dictionary<string, T>());
return;
}

Declarated[expression][name] = value;
}

public void Remove(BlockStatementExpression expression, string name)
{
if (expression is null)
Expand Down
9 changes: 5 additions & 4 deletions Paganism/Interpreter/Data/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public class Functions : DataStorage<FunctionInstance>
var parser = new Parser(lexer.Run(), files[0]);
var interpreter = new Interpreter(parser.Run());
interpreter.Run(false);
return new NoneValue();
return Value.NoneValue;
})
},
{ "pgm_resize", new FunctionInstance(
Expand All @@ -149,7 +150,7 @@ public class Functions : DataStorage<FunctionInstance>
{
if (i > array.Elements.Length - 1)
{
newElements[i] = new NoneValue();
newElements[i] = Value.NoneValue;
continue;
}
Expand Down Expand Up @@ -181,7 +182,7 @@ public class Functions : DataStorage<FunctionInstance>
}, false, true), (Argument[] arguments) =>
{
Console.WriteLine(arguments[0].Value.Eval().AsString());
return new NoneValue();
return Value.NoneValue;
}
)
},
Expand All @@ -192,7 +193,7 @@ public class Functions : DataStorage<FunctionInstance>
}, false, true), (Argument[] arguments) =>
{
Console.Write(arguments[0].Value.Eval().AsString());
return new NoneValue();
return Value.NoneValue;
}
)
},
Expand Down
7 changes: 4 additions & 3 deletions Paganism/Interpreter/Data/Structures.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Paganism.Interpreter.Data.Instances;
using Paganism.PParser.AST;
using Paganism.PParser.AST.Enums;
using Paganism.PParser.Values;
using System;
using System.Collections.Generic;

Expand All @@ -16,14 +17,14 @@ public class Structures : DataStorage<StructureInstance>
{
{ "task", new StructureInstance(new StructureDeclarateExpression(null, -1, -1, string.Empty, "task", new StructureMemberExpression[]
{
new StructureMemberExpression(null, -1, -1, string.Empty, "task", string.Empty, TypesType.Number, "id", true)
new StructureMemberExpression(null, -1, -1, string.Empty, "task", new TypeValue(TypesType.Number, string.Empty), "id", true)
}
))
},
{ "exception", new StructureInstance(new StructureDeclarateExpression(null, -1, -1, string.Empty, "exception", new StructureMemberExpression[]
{
new StructureMemberExpression(null, -1, -1, string.Empty, "exception", string.Empty, TypesType.String, "name", true),
new StructureMemberExpression(null, -1, -1, string.Empty, "exception", string.Empty, TypesType.String, "description", true)
new StructureMemberExpression(null, -1, -1, string.Empty, "exception", new TypeValue(TypesType.String, string.Empty), "name", true),
new StructureMemberExpression(null, -1, -1, string.Empty, "exception", new TypeValue(TypesType.String, string.Empty), "description", true)
}
))
}
Expand Down
1 change: 1 addition & 0 deletions Paganism/Interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Paganism.PParser.AST;
using System;

namespace Paganism.Interpreter
{
Expand Down
186 changes: 98 additions & 88 deletions Paganism/PParser/AST/BinaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,90 +25,6 @@ public BinaryOperatorExpression(BlockStatementExpression parent, int line, int p

public EvaluableExpression Right { get; }

public override Value Eval(params Argument[] arguments)
{
if (Type is BinaryOperatorType.Point)
{
return Point();
}
else if (Type is BinaryOperatorType.Assign)
{
return Assign();
}

var left = Left.Eval();
var right = Right.Eval();

return Type switch
{
BinaryOperatorType.Plus => Addition(left, right),
BinaryOperatorType.Minus => Minus(left, right),
BinaryOperatorType.Multiplicative => Multiplicative(left, right),
BinaryOperatorType.Division => Division(left, right),
BinaryOperatorType.Is => Is(left, right),
BinaryOperatorType.And => And(left, right),
BinaryOperatorType.Or => Or(left, right),
BinaryOperatorType.Less => Less(left, right),
BinaryOperatorType.More => More(left, right),
BinaryOperatorType.As => As(left, right),
_ => null,
};
}

private Value As(Value left, Value right)
{
return right is not TypeValue typeValue
? throw new InterpreterException("Right expression must be a type", Line, Position)
: typeValue.Value switch
{
TypesType.Any => new StringValue(left.AsString()),
TypesType.Number => new NumberValue(left.AsNumber()),
TypesType.String => new StringValue(left.AsString()),
TypesType.Boolean => new BooleanValue(left.AsBoolean()),
TypesType.Char => AsChar(left, right),
TypesType.None => new NoneValue(),
TypesType.Structure => AsStructure(left, typeValue),
_ => throw new InterpreterException($"You cant check type {left.Type} and {right.Type}"),
};
}

private Value AsStructure(Value left, TypeValue right)
{
if (left is not StructureValue structureValue || right.TypeName == string.Empty || right.TypeName is null)
{
throw new InterpreterException($"Cannot cast {left.Type} to Structure", Line, Position);
}

foreach (var member in structureValue.Structure.Members)
{
if (!member.Value.IsCastable)
{
continue;
}

var value = structureValue.Values[member.Key];

if (value is not StructureValue structureValue1)
{
continue;
}

if (structureValue1.Structure.Name != right.TypeName)
{
continue;
}

return structureValue1;
}

throw new InterpreterException($"Structure with '{structureValue.Structure.Name}' name havent castable member with '{right.TypeName}' type", Line, Position);
}

private Value AsChar(Value left, Value right)
{
return (left is StringValue stringValue && stringValue.Value.Length == 1) ? new CharValue(left.AsString()[0]) : throw new InterpreterException("Cannot cast string to char. String must be contains only one character.", Line, Position);
}

public static StructureValue GetStructure(BinaryOperatorExpression binaryOperatorExpression)
{
if (binaryOperatorExpression.Left is VariableExpression variableExpression)
Expand All @@ -133,7 +49,7 @@ public static StructureValue GetStructure(BinaryOperatorExpression binaryOperato
name = functionCallExpression.FunctionName;
}

if (!structure.Structure.Members[name].IsShow && structure.Structure.StructureDeclarateExpression.Filepath != binary.Filepath)
if (!structure.Structure.Members[name].Info.IsShow && structure.Structure.StructureDeclarateExpression.Filepath != binary.Filepath)
{
throw new InterpreterException($"You cant access to structure member '{name}' in '{structure.Structure.Name}' structure", binary.Line, binary.Position);
}
Expand Down Expand Up @@ -185,7 +101,7 @@ public static KeyValuePair<string, Value> GetMemberWithKeyOfStructure(BinaryOper

var member = structure.Values[name];

if (!structure.Structure.Members[name].IsShow && structure.Structure.StructureDeclarateExpression.Filepath != binaryOperatorExpression.Filepath)
if (!structure.Structure.Members[name].Info.IsShow && structure.Structure.StructureDeclarateExpression.Filepath != binaryOperatorExpression.Filepath)
{
throw new InterpreterException($"You cant access to structure member '{name}' in '{structure.Structure.Name}' structure", binaryOperatorExpression.Line, binaryOperatorExpression.Position);
}
Expand All @@ -208,6 +124,100 @@ public static Value GetMemberOfStructure(BinaryOperatorExpression binaryOperator
return GetMemberWithKeyOfStructure(binaryOperatorExpression).Value;
}

public override Value Eval(params Argument[] arguments)
{
if (Type is BinaryOperatorType.Point)
{
return Point();
}
else if (Type is BinaryOperatorType.Assign)
{
return Assign();
}

var left = Left.Eval();
var right = Right.Eval();

return Type switch
{
BinaryOperatorType.Plus => Addition(left, right),
BinaryOperatorType.Minus => Minus(left, right),
BinaryOperatorType.Multiplicative => Multiplicative(left, right),
BinaryOperatorType.Division => Division(left, right),
BinaryOperatorType.Is => Is(left, right),
BinaryOperatorType.And => And(left, right),
BinaryOperatorType.Or => Or(left, right),
BinaryOperatorType.Less => Less(left, right),
BinaryOperatorType.More => More(left, right),
BinaryOperatorType.As => As(left, right),
_ => null,
};
}

public TypeValue GetBinaryValueType()
{
if (Type is BinaryOperatorType.As)
{
return new TypeValue((Right as TypeExpression).Value, (Right as TypeExpression).TypeName);
}

return Left.GetTypeValue();
}

private Value As(Value left, Value right)
{
return right is not TypeValue typeValue
? throw new InterpreterException("Right expression must be a type", Line, Position)
: typeValue.Value switch
{
TypesType.Any => new StringValue(left.AsString()),
TypesType.Number => new NumberValue(left.AsNumber()),
TypesType.String => new StringValue(left.AsString()),
TypesType.Boolean => new BooleanValue(left.AsBoolean()),
TypesType.Char => AsChar(left, right),
TypesType.None => Value.NoneValue,
TypesType.Structure => AsStructure(left, typeValue),
_ => throw new InterpreterException($"You cant check type {left.Type} and {right.Type}"),
};
}

private Value AsStructure(Value left, TypeValue right)
{
if (left is not StructureValue structureValue || right.TypeName == string.Empty || right.TypeName is null)
{
throw new InterpreterException($"Cannot cast {left.Type} to Structure", Line, Position);
}

foreach (var member in structureValue.Structure.Members)
{
if (!member.Value.Info.IsCastable)
{
continue;
}

var value = structureValue.Values[member.Key];

if (value is not StructureValue structureValue1)
{
continue;
}

if (structureValue1.Structure.Name != right.TypeName)
{
continue;
}

return structureValue1;
}

throw new InterpreterException($"Structure with '{structureValue.Structure.Name}' name havent castable member with '{right.TypeName}' type", Line, Position);
}

private Value AsChar(Value left, Value right)
{
return (left is StringValue stringValue && stringValue.Value.Length == 1) ? new CharValue(left.AsString()[0]) : throw new InterpreterException("Cannot cast string to char. String must be contains only one character.", Line, Position);
}

private Value Point()
{
if (Left is VariableExpression variableExpression && Interpreter.Data.Enums.Instance.Value.TryGet(Parent, variableExpression.Name, out var value))
Expand Down Expand Up @@ -386,10 +396,10 @@ public Value Assign()

if (value is FunctionValue)
{
Functions.Instance.Value.Add(variableExpression.Parent, variableExpression.Name, new FunctionInstance(Right as FunctionDeclarateExpression));
Functions.Instance.Value.Set(variableExpression.Parent, variableExpression.Name, new FunctionInstance(Right as FunctionDeclarateExpression));
}

Variables.Instance.Value.Add(variableExpression.Parent, variableExpression.Name, value);
Variables.Instance.Value.Set(variableExpression.Parent, variableExpression.Name, value);
}
else if (Left is BinaryOperatorExpression binary)
{
Expand Down
10 changes: 5 additions & 5 deletions Paganism/PParser/AST/BlockStatementExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Paganism.PParser.AST
{
public class BlockStatementExpression : Expression, IStatement, IExecutable
public class BlockStatementExpression : EvaluableExpression, IStatement, IExecutable
{
public BlockStatementExpression(BlockStatementExpression parent, int line, int position, string filepath, IStatement[] statements, bool isLoop = false, bool isClearing = true) : base(parent, line, position, filepath)
{
Expand All @@ -23,10 +23,10 @@ public BlockStatementExpression(BlockStatementExpression parent, int line, int p

public void Execute(params Argument[] arguments)
{
ExecuteAndReturn(arguments);
Eval(arguments);
}

public Value ExecuteAndReturn(params Argument[] arguments)
public override Value Eval(params Argument[] arguments)
{
if (Statements == null)
{
Expand Down Expand Up @@ -114,7 +114,7 @@ public Value ExecuteAndReturn(params Argument[] arguments)

if (variable != null)
{
Variables.Instance.Value.Add(forExpression.Parent, (variable.Left as VariableExpression).Name, variable.Right.Eval());
Variables.Instance.Value.Set(forExpression.Parent, (variable.Left as VariableExpression).Name, variable.Right.Eval());
}

var result2 = forExpression.Eval();
Expand All @@ -135,7 +135,7 @@ public Value ExecuteAndReturn(params Argument[] arguments)
{
Variables.Instance.Value.Clear(this);
Functions.Instance.Value.Clear(this);
Structures.Instance.Value.Clear(this);
Interpreter.Data.Structures.Instance.Value.Clear(this);
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion Paganism/PParser/AST/EnumDeclarateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public EnumDeclarateExpression(BlockStatementExpression parent, int position, in

public void Declarate()
{
Interpreter.Data.Enums.Instance.Value.Add(Parent, Name, new EnumInstance(this));
Interpreter.Data.Enums.Instance.Value.Set(Parent, Name, new EnumInstance(this));
}

public void Remove()
Expand Down
Loading

0 comments on commit ee7a238

Please sign in to comment.