Skip to content

Commit

Permalink
complete type loading system rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Oct 10, 2023
1 parent af642d6 commit 8c44d94
Show file tree
Hide file tree
Showing 65 changed files with 760 additions and 926 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override IAddress Execute(VirtualMachine vm)
{
var frame = vm.Frames.Peek();
frame[Left] = JsonSerializer.Serialize(
right.right.Get(frame),
Right.right.Get(frame),
new JsonSerializerOptions
{
WriteIndented = true,
Expand All @@ -28,5 +28,5 @@ public override IAddress Execute(VirtualMachine vm)
}

protected override string ToStringInternal() =>
$"{Left} = {right.right} as string";
$"{Left} = {Right.right} as string";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public override IAddress Execute(VirtualMachine vm)
{
var frame = vm.Frames.Peek();
var obj = (Dictionary<string, object>) frame[Left];
var field = (string) right.left.Get(frame) ?? string.Empty;
obj[field] = right.right.Get(frame);
var field = (string) Right.left.Get(frame) ?? string.Empty;
obj[field] = Right.right.Get(frame);
return Address.Next;
}

public Simple ToSimple() =>
new DotRead(new Name(Left), right.left);
new DotRead(new Name(Left), Right.left);

protected override string ToStringInternal() =>
$"{Left}.{right.left} = {right.right}";
$"{Left}.{Right.left} = {Right.right}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public override IAddress Execute(VirtualMachine vm)
{
var frame = vm.Frames.Peek();
var obj = (List<object>) frame[Left];
var index = Convert.ToInt32(right.left.Get(frame));
obj[index] = right.right.Get(frame);
var index = Convert.ToInt32(Right.left.Get(frame));
obj[index] = Right.right.Get(frame);
return Address.Next;
}

public Simple ToSimple() =>
new IndexRead(new Name(Left), right.left);
new IndexRead(new Name(Left), Right.left);

protected override string ToStringInternal() =>
$"{Left}[{right.left}] = {right.right}";
$"{Left}[{Right.left}] = {Right.right}";
}
20 changes: 10 additions & 10 deletions Interpreter.Lib/BackEnd/Instructions/WithAssignment/Simple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class Simple : Instruction
{
public string Left { get; set; }

protected (IValue left, IValue right) right;
protected (IValue left, IValue right) Right;
private readonly string _operator;

public Simple(string left,
(IValue left, IValue right) right,
string @operator)
{
Left = left;
this.right = right;
Right = right;
_operator = @operator;
}

Expand Down Expand Up @@ -43,21 +43,21 @@ protected override void OnSetOfAddress(IAddress address) =>
public override IAddress Execute(VirtualMachine vm)
{
var frame = vm.Frames.Peek();
if (right.left == null)
if (Right.left == null)
{
var value = right.right.Get(frame);
var value = Right.right.Get(frame);
frame[Left] = _operator switch
{
"-" => -Convert.ToDouble(value),
"!" => !Convert.ToBoolean(value),
"~" => ((List<object>) value).Count,
"" => value,
_ => throw new NotImplementedException()
_ => throw new NotSupportedException($"_operator {_operator} is not supported")
};
}
else
{
object lValue = right.left.Get(frame), rValue = right.right.Get(frame);
object lValue = Right.left.Get(frame), rValue = Right.right.Get(frame);
frame[Left] = _operator switch
{
"+" when lValue is string => lValue.ToString() + rValue,
Expand All @@ -77,7 +77,7 @@ public override IAddress Execute(VirtualMachine vm)
"." => ((Dictionary<string, object>) lValue)[rValue.ToString()!],
"[]" => ((List<object>) lValue)[Convert.ToInt32(rValue)],
"++" => ((List<object>) lValue).Concat((List<object>) rValue).ToList(),
_ => throw new NotImplementedException()
_ => throw new NotSupportedException($"_operator {_operator} is not supported")
};
}
if (vm.CallStack.Any())
Expand All @@ -97,7 +97,7 @@ public override IAddress Execute(VirtualMachine vm)
return Address.Next;
}

protected override string ToStringInternal() => right.left == null
? $"{Left} = {_operator}{right.right}"
: $"{Left} = {right.left} {_operator} {right.right}";
protected override string ToStringInternal() => Right.left == null
? $"{Left} = {_operator}{Right.right}"
: $"{Left} = {Right.left} {_operator} {Right.right}";
}
105 changes: 53 additions & 52 deletions Interpreter.Lib/FrontEnd/TopDownParse/Impl/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
using Interpreter.Lib.IR.Ast.Impl.Nodes.Expressions.ComplexLiterals;
using Interpreter.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;
using Interpreter.Lib.IR.Ast.Impl.Nodes.Statements;
using Interpreter.Lib.IR.CheckSemantics.Types;
using Interpreter.Lib.IR.CheckSemantics.Variables.Symbols;

namespace Interpreter.Lib.FrontEnd.TopDownParse.Impl;

Expand Down Expand Up @@ -208,44 +206,47 @@ private TypeDeclaration TypeDeclaration()
Expect("Assign");
var type = TypeValue();

type.Recursive = type.ToString().Contains(ident.Value);

return new TypeDeclaration(ident.Value, type) { Segment = typeWord.Segment + ident.Segment };
}

private Type TypeValue()
private TypeValue TypeValue()
{
if (CurrentIs("Ident"))
{
var ident = Expect("Ident");
var identType = new Type(ident.Value);
var identType = new TypeIdentValue(ident.Value);

return WithSuffix(identType);
}

if (CurrentIs("LeftCurl"))
{
Expect("LeftCurl");
var propertyTypes = new List<PropertyType>();
var propertyTypes = new List<PropertyTypeValue>();
while (CurrentIs("Ident"))
{
var ident = Expect("Ident");
Expect("Colon");
var propType = TypeValue();
propertyTypes.Add(new PropertyType(ident.Value, propType));
propertyTypes.Add(
new PropertyTypeValue(
ident.Value,
propType));
Expect("SemiColon");
}

Expect("RightCurl");

return WithSuffix(new ObjectType(propertyTypes));
return WithSuffix(new ObjectTypeValue(propertyTypes));
}

if (CurrentIs("LeftParen"))
{
Expect("LeftParen");
var args = new List<Type>();
while (CurrentIs("Ident") || CurrentIs("LeftCurl") || CurrentIs("LeftParen"))
var args = new List<TypeValue>();
while (CurrentIs("Ident") ||
CurrentIs("LeftCurl") ||
CurrentIs("LeftParen"))
{
args.Add(TypeValue());
if (!CurrentIs("RightParen"))
Expand All @@ -256,13 +257,13 @@ private Type TypeValue()
Expect("RightParen");
Expect("Arrow");
var returnType = TypeValue();
return new FunctionType(returnType, args);
return new FunctionTypeValue(returnType, args);
}

return null;
}

private Type WithSuffix(Type baseType)
private TypeValue WithSuffix(TypeValue baseType)
{
var type = baseType;
while (CurrentIs("LeftBracket") || CurrentIs("QuestionMark"))
Expand All @@ -271,12 +272,12 @@ private Type WithSuffix(Type baseType)
{
Expect("LeftBracket");
Expect("RightBracket");
type = new ArrayType(type);
type = new ArrayTypeValue(type);
}
else if (CurrentIs("QuestionMark"))
{
Expect("QuestionMark");
type = new NullableType(type);
type = new NullableTypeValue(type);
}
}

Expand Down Expand Up @@ -309,13 +310,13 @@ private FunctionDeclaration FunctionDeclaration()
var ident = Expect("Ident");

Expect("LeftParen");
var args = new List<VariableSymbol>();
var args = new List<PropertyTypeValue>();
if (CurrentIs("Ident"))
{
var arg = Expect("Ident").Value;
Expect("Colon");
var type = TypeValue();
args.Add(new VariableSymbol(arg, type));
args.Add(new PropertyTypeValue(arg, type));
}

while (CurrentIs("Comma"))
Expand All @@ -324,24 +325,19 @@ private FunctionDeclaration FunctionDeclaration()
var arg = Expect("Ident").Value;
Expect("Colon");
var type = TypeValue();
args.Add(new VariableSymbol(arg, type));
args.Add(new PropertyTypeValue(arg, type));
}

Expect("RightParen");

var returnType = TypeUtils.JavaScriptTypes.Void;
TypeValue returnType = new TypeIdentValue(TypeId: "undefined");
if (CurrentIs("Colon"))
{
Expect("Colon");
returnType = TypeValue();
}

var functionSymbol =
new FunctionSymbol(ident.Value, args,
new FunctionType(returnType, args.Select(x => x.Type))
);

return new FunctionDeclaration(functionSymbol, BlockStatement())
return new FunctionDeclaration(ident.Value, returnType, args, BlockStatement())
{ Segment = ident.Segment };
}

Expand Down Expand Up @@ -390,13 +386,11 @@ private void AddToDeclaration(LexicalDeclaration declaration)
}
else
{
var expression = new Literal(
type, TypeUtils.GetDefaultValue(type),
label: TypeUtils.GetDefaultValue(type) == null ? "null" : null
);
var expression = new ImplicitLiteral(type);
assignment = new AssignmentExpression(
new MemberExpression(identRef),
expression, type);
lhs: new MemberExpression(identRef),
expression,
type);
}
}
declaration.AddAssignment(assignment);
Expand Down Expand Up @@ -672,26 +666,36 @@ private Literal Literal()
{
var str = Expect("StringLiteral");
return new Literal(
TypeUtils.JavaScriptTypes.String,
Regex.Unescape(str.Value.Trim('"')),
new TypeIdentValue(TypeId: "string"),
value: Regex.Unescape(str.Value.Trim('"')),
segment,
str.Value
label: str.Value
.Replace(@"\", @"\\")
.Replace(@"""", @"\""")
);
.Replace(@"""", @"\"""));
}

return _tokens.Current.Type.Tag switch
{
"NullLiteral" => new Literal(TypeUtils.JavaScriptTypes.Null,
Expect("NullLiteral").Value == "null" ? null : "", segment, "null"),
"IntegerLiteral" => new Literal(TypeUtils.JavaScriptTypes.Number,
double.Parse(Expect("IntegerLiteral").Value), segment),
"FloatLiteral" => new Literal(TypeUtils.JavaScriptTypes.Number,
double.Parse(Expect("FloatLiteral").Value, CultureInfo.InvariantCulture), segment),
"BooleanLiteral" => new Literal(TypeUtils.JavaScriptTypes.Boolean,
bool.Parse(Expect("BooleanLiteral").Value), segment),
_ => new Literal(TypeUtils.JavaScriptTypes.Undefined, new TypeUtils.Undefined())
"NullLiteral" => new Literal(
new TypeIdentValue(TypeId: "null"),
Expect("NullLiteral").Value == "null" ? null : string.Empty,
segment,
label: "null"),
"IntegerLiteral" => new Literal(
new TypeIdentValue(TypeId: "number"),
value: double.Parse(Expect("IntegerLiteral").Value),
segment),
"FloatLiteral" => new Literal(
new TypeIdentValue(TypeId: "number"),
value: double.Parse(
Expect("FloatLiteral").Value,
CultureInfo.InvariantCulture),
segment),
"BooleanLiteral" => new Literal(
new TypeIdentValue(TypeId: "boolean"),
value: bool.Parse(Expect("BooleanLiteral").Value),
segment),
_ => throw new ParserException("There are no more supported literals")
};
}

Expand All @@ -715,30 +719,27 @@ private ObjectLiteral ObjectLiteral()
{
Expect("Arrow");
Expect("LeftParen");
var args = new List<VariableSymbol>();
var args = new List<PropertyTypeValue>();
while (CurrentIs("Ident"))
{
var name = Expect("Ident").Value;
Expect("Colon");
var type = TypeValue();
args.Add(new VariableSymbol(name, type));
args.Add(new PropertyTypeValue(name, type));
if (!CurrentIs("RightParen"))
{
Expect("Comma");
}
}
Expect("RightParen");
var returnType = TypeUtils.JavaScriptTypes.Void;
TypeValue returnType = new TypeIdentValue(TypeId: "undefined");
if (CurrentIs("Colon"))
{
Expect("Colon");
returnType = TypeValue();
}

var functionSymbol = new FunctionSymbol(idToken.Value, args,
new FunctionType(returnType, args.Select(a => a.Type))
);
methods.Add(new FunctionDeclaration(functionSymbol, BlockStatement())
methods.Add(new FunctionDeclaration(idToken.Value, returnType, args, BlockStatement())
{ Segment = idToken.Segment }
);
}
Expand Down
2 changes: 1 addition & 1 deletion Interpreter.Lib/FrontEnd/TopDownParse/ParserException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ParserException : Exception
{
public ParserException() { }

protected ParserException(string message) : base(message) { }
public ParserException(string message) : base(message) { }

protected ParserException(string message, Exception inner) : base(message, inner) { }

Expand Down
Loading

0 comments on commit 8c44d94

Please sign in to comment.