Skip to content

Commit

Permalink
feat: Add typeof expression
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 29, 2024
1 parent f324b0a commit 3793194
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
11 changes: 0 additions & 11 deletions NewSource/Socordia.CodeAnalysis/AST/Expressions/TypeOf.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Socordia.CodeAnalysis.AST.TypeNames;

namespace Socordia.CodeAnalysis.AST.Expressions;

public class TypeOfExpression : AstNode
{
public TypeOfExpression(TypeName type)
{
Properties.Set(nameof(Type), type);
}

public TypeName Type => Properties.GetOrThrow<TypeName>(nameof(Type));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Expressions;

namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Expressions;

Expand All @@ -12,6 +13,6 @@ public static AstNode Parse(TokenIterator iterator, Parser parser)

iterator.Match(TokenType.CloseParen);

return SyntaxTree.TypeOfExpression(type);
return new TypeOfExpression(type);
}
}
5 changes: 0 additions & 5 deletions NewSource/Socordia.CodeAnalysis/Parsing/SyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ public static AstNode Unit(AstNode value, string unit)
return new UnitLiteral(value, unit);
}

public static AstNode TypeOfExpression(AstNode type)
{
return new TypeOf(type);
}

public static AstNode DoWhile(Block body, AstNode cond)
{
return new DoWhileStatement(body, cond);
Expand Down
14 changes: 14 additions & 0 deletions NewSource/SocordiaC/Compilation/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Socordia.CodeAnalysis.AST.Literals;
using Socordia.CodeAnalysis.AST.TypeNames;
using Socordia.CodeAnalysis.Parsing;
using Socordia.CodeAnalysis.Parsing.ParsePoints.Expressions;
using SocordiaC.Compilation.Body;

namespace SocordiaC.Compilation;
Expand Down Expand Up @@ -176,10 +177,23 @@ public static Value CreateValue(AstNode valueNode, BodyCompilation compilation)
LiteralNode literal => CreateLiteral(literal.Value),
DefaultLiteral def => CreateDefault(def, compilation),
CallExpression call => CreateCall(call, compilation),
TypeOfExpression typeOf => CreateTypeOf(typeOf, compilation),
_ => throw new NotImplementedException()
};
}

private static Value CreateTypeOf(TypeOfExpression typeOf, BodyCompilation compilation)
{
var type = GetTypeFromNode(typeOf.Type, compilation.Driver.Compilation.Module)!;

var ldToken = compilation.Builder.Emit(new CilIntrinsic.LoadHandle(compilation.Method.Module.Resolver, type));

var typeType = compilation.Builder.Resolver.Import(typeof(Type));
var fromTokenMethod = typeType.FindMethod("GetTypeFromHandle")!;

return new CallInst(fromTokenMethod, [ldToken]);
}

private static Value CreateCall(CallExpression call, BodyCompilation compilation)
{
var listener = new CallExpressionListener(false);
Expand Down
1 change: 1 addition & 0 deletions NewSource/SocordiaC/compilation.sc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func unit(x: unit cm): none
func main(): none {
let myFlag = true;
let t = test(1, false);
let t : System.Type = typeof(i32);

print("hello");
println("world");
Expand Down

0 comments on commit 3793194

Please sign in to comment.