Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SpGerg committed Feb 1, 2024
1 parent e36abdf commit 4ef6eb1
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 84 deletions.
Binary file modified Imports/PaganismCustomConsole/Paganism.dll
Binary file not shown.
5 changes: 5 additions & 0 deletions Paganism/Interpreter/Data/DataStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public T Get(BlockStatementExpression expression, string name)
Declarated.Add(expression, new Dictionary<string, T>());
}

if (Declarated[expression].TryGetValue(name, out var result3))
{
return result3;
}

if (!Language.TryGetValue(name, out var result) && !Declarated[expression].TryGetValue(name, out var result1))
{
var value = Get(expression.Parent, name);
Expand Down
3 changes: 2 additions & 1 deletion Paganism/PParser/AST/BinaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override Value Eval(params Argument[] arguments)
{
BinaryOperatorType.Plus => Addition(left, right),
BinaryOperatorType.Minus => Minus(left, right),
BinaryOperatorType.Multiplicative => Addition(left, right),
BinaryOperatorType.Multiplicative => Multiplicative(left, right),
BinaryOperatorType.Division => Division(left, right),
BinaryOperatorType.Is => Is(left, right),
BinaryOperatorType.And => And(left, right),
Expand Down Expand Up @@ -283,6 +283,7 @@ public Value Addition(Value left, Value right)
TypesType.Number => new NumberValue(left.AsNumber() + right.AsNumber()),
TypesType.String => new StringValue(left.AsString() + right.AsString()),
TypesType.Type => new StringValue(left.AsString() + right.AsString()),
TypesType.None => new StringValue(left.AsString() + right.AsString()),
_ => throw new InterpreterException($"You cant addition type {left.Type} and {right.Type}"),
};
}
Expand Down
102 changes: 32 additions & 70 deletions Paganism/PParser/AST/FunctionDeclarateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public FunctionDeclarateExpression(BlockStatementExpression parent, int line, in

public TypeValue ReturnType { get; }

private bool IsChecked { get; set; }
private bool _isChecked { get; set; }

private static readonly Dictionary<string, Type> Types = new()
{
Expand Down Expand Up @@ -137,7 +137,7 @@ public void CreateArguments(params Argument[] arguments)

try
{
value = Variables.Instance.Value.Get(Statement, argument.Name);
value = Variables.Instance.Value.Get(Statement, functionArgument.Name);
}
catch
{
Expand Down Expand Up @@ -167,82 +167,32 @@ public void CreateArguments(params Argument[] arguments)

totalArguments[i] = initArgument;

if (initArgument.Value is FunctionDeclarateExpression functionDeclarateExpression)
if (initArgument.Value is FunctionCallExpression function)
{
Variables.Instance.Value.Add(Statement, initArgument.Name, new FunctionValue(functionDeclarateExpression));
Functions.Instance.Value.Add(Statement, initArgument.Name, new FunctionInstance(functionDeclarateExpression));
initArgument.Value = function.Eval(function.Arguments);
}
else
else if (initArgument.Value is BinaryOperatorExpression operatorExpression)
{
Variables.Instance.Value.Add(Statement, initArgument.Name, initArgument.Value.Eval());
}
initArgument.Value = operatorExpression.Eval();
}
}
}

public override Value Eval(params Argument[] arguments)
{
if (!IsChecked && Statement.Statements is not null)
foreach (var argument in totalArguments)
{
var statements = Statement.Statements.Where(statement => statement is ReturnExpression);

if (statements.Count() != 0)
if (argument.Value is FunctionDeclarateExpression functionDeclarateExpression)
{
foreach (ReturnExpression returnExpression in statements)
{
if (returnExpression.Value is FunctionCallExpression function && function.GetFunction().ReturnType != ReturnType)
{
throw new InterpreterException($"Except return {ReturnType.AsString()} type", returnExpression.Line, returnExpression.Position);
}

if (ReturnType.Value is not TypesType.Array)
{
if (returnExpression.Value is ArrayExpression)
{
throw new InterpreterException($"Didnt except array", returnExpression.Line, returnExpression.Position);
}

if (returnExpression.Value is ArrayElementExpression elementExpression && elementExpression.Eval().Type is TypesType.Array)
{
throw new InterpreterException($"Didnt except array", returnExpression.Line, returnExpression.Position);
}
}

if (ReturnType.Value is TypesType.Array)
{
if (returnExpression.Value is not ArrayExpression)
{
throw new InterpreterException($"Except array", returnExpression.Line, returnExpression.Position);
}

if (returnExpression.Value is ArrayElementExpression elementExpression && elementExpression.Eval().Type is not TypesType.Array)
{
throw new InterpreterException($"Except array", returnExpression.Line, returnExpression.Position);
}
}

var value = returnExpression.Value.Eval();

if (ReturnType.Value != value.Type)
{
throw new InterpreterException($"Except {ReturnType.AsString()}", returnExpression.Line, returnExpression.Position);
}

if (value is StructureValue structureValue && structureValue.Structure.Name != ReturnType.TypeName)
{
throw new InterpreterException($"Except {ReturnType.AsString()}", returnExpression.Line, returnExpression.Position);
}

if (value is EnumValue enumValue && enumValue.Member.Enum != ReturnType.TypeName)
{
throw new InterpreterException($"Except {ReturnType.AsString()}", returnExpression.Line, returnExpression.Position);
}
}
Variables.Instance.Value.Add(Statement, argument.Name, new FunctionValue(functionDeclarateExpression));
Functions.Instance.Value.Add(Statement, argument.Name, new FunctionInstance(functionDeclarateExpression));
}
else
{
Variables.Instance.Value.Add(Statement, argument.Name, argument.Value.Eval());
}

IsChecked = true;
}
}

public override Value Eval(params Argument[] arguments)
{
CreateArguments(arguments);

if (Name == "pgm_call")
Expand Down Expand Up @@ -348,7 +298,7 @@ public override Value Eval(params Argument[] arguments)
interpreter.Run(false);
}

if (Statement == null)
if (Statement is null)
{
return new NoneValue();
}
Expand All @@ -368,7 +318,19 @@ public override Value Eval(params Argument[] arguments)
}
else
{
return Statement.ExecuteAndReturn(arguments);
var result = Statement.ExecuteAndReturn(arguments);

if (result is null)
{
return new NoneValue();
}

if (!result.IsType(ReturnType))
{
throw new InterpreterException($"Except {ReturnType.AsString()} type");
}

return result;
}
}
}
Expand Down
15 changes: 3 additions & 12 deletions Paganism/PParser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ private StructureMemberExpression ParseDelegate(string structureName, bool isSho

private IStatement ParseFor()
{
Match(TokenType.For);

if (!Match(TokenType.LeftPar))
{
throw new ParserException("Except '('.", Current.Line, Current.Position);
Expand All @@ -344,7 +346,7 @@ private IStatement ParseFor()

if (!Require(0, TokenType.Semicolon))
{
variable = ParseVariable();
variable = ParseVariable(false, ParseType());
}

if (!Match(TokenType.Semicolon))
Expand Down Expand Up @@ -460,7 +462,6 @@ private IStatement ParseFunctionOrVariable(bool isShow = false)
}
else
{
Position--;
return ParseVariable(isShow, type);
}
}
Expand All @@ -486,16 +487,6 @@ private IStatement ParseVariable(bool isShow = false, TypeValue type = null)
return binaryOperatorExpression;
}

if (Match(TokenType.LeftBracket))
{
isArray = true;

if (!Match(TokenType.RightBracket))
{
throw new ParserException("Except ].", Current.Line, Current.Position);
}
}

Match(TokenType.Assign);

if (isArray)
Expand Down
2 changes: 1 addition & 1 deletion Paganism/PParser/Values/StructureValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Set(string key, Value value)

var member = Structure.Members[key];

if (member.Type != value.Type && (value is TypeValue typeValue && typeValue.Value is not TypesType.None))
if (member.Type != TypesType.Any && member.Type != value.Type && (value is TypeValue typeValue && typeValue.Value is not TypesType.None))
{
throw new InterpreterException($"Except {member.GetRequiredType()} type");
}
Expand Down
70 changes: 70 additions & 0 deletions Paganism/PParser/Values/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,76 @@ public static Value Create(Value copy)
return null;
}

public bool IsType(Value value)
{
if (value.Type is TypesType.Any)
{
return true;
}

if (value.Type != Type && value.Type != TypesType.Type)
{
return false;
}

if (value is StructureValue structureValue)
{
if (this is not StructureValue structureValue1)
{
return false;
}

if (structureValue1.Structure.Name != structureValue.Structure.Name)
{
return false;
}
}

if (value is EnumValue enumValue)
{
if (this is not EnumValue enumValue1)
{
return false;
}

if (enumValue.Member.Enum != enumValue1.Member.Enum)
{
return false;
}
}

if (value is TypeValue typeValue)
{
if (typeValue.Value is TypesType.Structure)
{
if (this is not StructureValue structureValue1)
{
return false;
}

if (structureValue1.Structure.Name != typeValue.TypeName)
{
return false;
}
}

if (typeValue.Value is TypesType.Enum)
{
if (this is not EnumValue enumValue1)
{
return false;
}

if (enumValue1.Member.Enum != typeValue.TypeName)
{
return false;
}
}
}

return true;
}

public override Value Eval(params Argument[] arguments)
{
return this;
Expand Down

0 comments on commit 4ef6eb1

Please sign in to comment.