Skip to content

Commit

Permalink
night things
Browse files Browse the repository at this point in the history
  • Loading branch information
SpGerg committed Feb 5, 2024
1 parent b3f0667 commit b401d5d
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 16 deletions.
Binary file modified Imports/PaganismCustomConsole/Paganism.dll
Binary file not shown.
19 changes: 15 additions & 4 deletions Paganism/Interpreter/Data/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ public class Functions : DataStorage<FunctionInstance>

protected override IReadOnlyDictionary<string, FunctionInstance> Language { get; } = new Dictionary<string, FunctionInstance>()
{
{ "pgm_call", new FunctionInstance(
{ "cs_call", new FunctionInstance(
new FunctionDeclarateExpression(null, -1, -1, string.Empty, "pgm_call", new BlockStatementExpression(null, 0, 0, string.Empty, null), new Argument[]
{
new Argument("namespace", TypesType.String),
new Argument("method", TypesType.String),
new Argument("arguments", TypesType.Any)
new Argument("namespace", TypesType.String, null, true),
new Argument("method", TypesType.String, null, true),
new Argument("arguments", TypesType.Any, null, true, true)
},
false,
true)
)
},
{ "cs_create", new FunctionInstance(
new FunctionDeclarateExpression(null, -1, -1, string.Empty, "cs_create", new BlockStatementExpression(null, 0, 0, string.Empty, null), new Argument[]
{
new Argument("namespace", TypesType.String, null, true),
new Argument("class", TypesType.String, null, true),
new Argument("arguments", TypesType.Any, null, true, true)
},
false,
true)
Expand Down
3 changes: 2 additions & 1 deletion Paganism/Lexer/Enums/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public enum TokenType
Catch = 56,
Enum = 57,
New = 58,
EnumType = 59
EnumType = 59,
Readonly = 60
}
}
3 changes: 2 additions & 1 deletion Paganism/Lexer/Tokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static class Tokens
{ "catch", TokenType.Catch },
{ "enum", TokenType.Enum },
{ "new", TokenType.New },
{ "enum_type", TokenType.EnumType }
{ "enum_type", TokenType.EnumType },
{ "readonly", TokenType.Readonly },
};

public static IReadOnlyDictionary<string, TokenType> OperatorsType { get; } = new Dictionary<string, TokenType>()
Expand Down
2 changes: 1 addition & 1 deletion Paganism/PParser/AST/BinaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public Value Assign()
var structure = GetStructure(binary);
var member = GetMemberWithKeyOfStructure(binary);

structure.Set(member.Key, value);
structure.Set(member.Key, value, Filepath);
}
else if (Left is ArrayElementExpression arrayElementExpression)
{
Expand Down
2 changes: 1 addition & 1 deletion Paganism/PParser/AST/FunctionDeclarateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public override Value Eval(params Argument[] arguments)
structureExpression.Parent, structureExpression.Line, structureExpression.Position, Filepath, structureExpression.Name, string.Empty, TypesType.Number, "id", true);

var structure = Value.Create(structureExpression) as StructureValue;
structure.Set("id", new NumberValue(task.Id));
structure.Set("id", new NumberValue(task.Id), Filepath);

return structure;
}
Expand Down
5 changes: 4 additions & 1 deletion Paganism/PParser/AST/StructureMemberExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace Paganism.PParser.AST
public class StructureMemberExpression : Expression, IStatement
{
public StructureMemberExpression(BlockStatementExpression parent, int line, int position, string filepath, string structure, string typeName, TypesType type, string name,
bool isShow = false, bool isAsync = false, bool isDelegate = false, Argument[] arguments = null, bool isCastable = false) : base(parent, line, position, filepath)
bool isShow = false, bool isReadOnly = false, bool isAsync = false, bool isDelegate = false, Argument[] arguments = null, bool isCastable = false) : base(parent, line, position, filepath)
{
Structure = structure;
TypeName = typeName;
Type = type;
Name = name;
IsShow = isShow;
IsReadOnly = isReadOnly;
IsAsync = isAsync;
IsDelegate = isDelegate;
Arguments = arguments;
Expand All @@ -32,6 +33,8 @@ public StructureMemberExpression(BlockStatementExpression parent, int line, int

public Argument[] Arguments { get; }

public bool IsReadOnly { get; }

public bool IsShow { get; }

public bool IsCastable { get; }
Expand Down
4 changes: 2 additions & 2 deletions Paganism/PParser/AST/TryCatchExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public override Value Eval(params Argument[] arguments)
catch (Exception exception)
{
var structure = new StructureValue(Parent, Structures.Instance.Value.Get(null, "exception"));
structure.Set("name", new StringValue(exception.GetType().Name));
structure.Set("description", new StringValue(exception.Message));
structure.Set("name", new StringValue(exception.GetType().Name), Filepath);
structure.Set("description", new StringValue(exception.Message), Filepath);

Variables.Instance.Value.Add(CatchExpression, "exception", structure);

Expand Down
9 changes: 5 additions & 4 deletions Paganism/PParser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,14 @@ private TypeValue ParseType()
private StructureMemberExpression ParseStructureMember(string structureName)
{
var isShow = Match(TokenType.Show) ? true : !Match(TokenType.Hide);
var isReadOnly = Match(TokenType.Readonly);
var isCastable = Match(TokenType.Castable);

var current = Current.Type;

if (Require(0, TokenType.Delegate))
{
var member2 = ParseDelegate(structureName, isShow, isCastable);
var member2 = ParseDelegate(structureName, isShow, isReadOnly, isCastable);

return member2;
}
Expand All @@ -296,12 +297,12 @@ private StructureMemberExpression ParseStructureMember(string structureName)
throw new ParserException("Except structure member name.", Current.Line, Current.Position);
}

var member = new StructureMemberExpression(_parent, Current.Line, Current.Position, Filepath, structureName, type.TypeName, Lexer.Tokens.TokenTypeToValueType[current], memberName, isShow, isCastable);
var member = new StructureMemberExpression(_parent, Current.Line, Current.Position, Filepath, structureName, type.TypeName, Lexer.Tokens.TokenTypeToValueType[current], memberName, isShow, isReadOnly, isCastable);

return !Match(TokenType.Semicolon) ? throw new ParserException("Except ';'.", Current.Line, Current.Position) : member;
}

private StructureMemberExpression ParseDelegate(string structureName, bool isShow, bool isCastable)
private StructureMemberExpression ParseDelegate(string structureName, bool isShow, bool isReadOnly, bool isCastable)
{
Match(TokenType.Delegate);

Expand All @@ -328,7 +329,7 @@ private StructureMemberExpression ParseDelegate(string structureName, bool isSho
Match(TokenType.Semicolon);

return new StructureMemberExpression(_parent, Current.Line, Current.Position, Filepath, structureName, type.TypeName, current is TokenType.Function ? TypesType.None : Lexer.Tokens.TokenTypeToValueType[current], memberName,
isShow, isAsync, true, arguments, isCastable);
isShow, isReadOnly, isAsync, true, arguments, isCastable);
}

private IStatement ParseFor()
Expand Down
5 changes: 5 additions & 0 deletions Paganism/PParser/Values/NumberValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public override string AsString()
{
return Value.ToString();
}

public override bool AsBoolean()
{
return Value == 1 ? true : false;
}
}
}
7 changes: 6 additions & 1 deletion Paganism/PParser/Values/StructureValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public StructureValue(BlockStatementExpression expression, string name) : this(S

public BlockStatementExpression BlockStatement { get; }

public void Set(string key, Value value)
public void Set(string key, Value value, string filePath)
{
if (!Values.ContainsKey(key))
{
Expand All @@ -60,6 +60,11 @@ public void Set(string key, Value value)

var member = Structure.Members[key];

if (member.IsReadOnly && filePath != member.Filepath)
{
throw new InterpreterException($"You cant access to structure member '{key}' in '{Structure.Name}' structure");
}

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

0 comments on commit b401d5d

Please sign in to comment.