-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add compiling VariableStatement
- Loading branch information
Showing
10 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Socordia.CodeAnalysis.AST; | ||
|
||
public class EmptyNode : AstNode | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Socordia.CodeAnalysis.AST.TypeNames; | ||
|
||
public class NoTypeName : TypeName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using DistIL.AsmIO; | ||
using DistIL.IR.Utils; | ||
using MrKWatkins.Ast.Listening; | ||
using Socordia.CodeAnalysis.AST; | ||
|
||
namespace SocordiaC.Compilation.Body; | ||
|
||
public record BodyCompilation(Driver Driver, MethodDef Method, IRBuilder Builder) | ||
{ | ||
public static CompositeListener<BodyCompilation, AstNode> Listener = | ||
CompositeListener<BodyCompilation, AstNode>.Build() | ||
.With(new VariableDeclarationListener()) | ||
.ToListener(); | ||
} |
33 changes: 33 additions & 0 deletions
33
NewSource/SocordiaC/Compilation/Body/VariableDeclarationListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using DistIL.AsmIO; | ||
using MrKWatkins.Ast.Listening; | ||
using Socordia.CodeAnalysis.AST; | ||
using Socordia.CodeAnalysis.AST.Statements; | ||
using Socordia.CodeAnalysis.AST.TypeNames; | ||
|
||
namespace SocordiaC.Compilation.Body; | ||
|
||
public class VariableDeclarationListener : Listener<BodyCompilation, AstNode, VariableStatement> | ||
{ | ||
protected override void ListenToNode(BodyCompilation context, VariableStatement node) | ||
{ | ||
var value = Utils.CreateValue(node.Initializer); | ||
|
||
TypeDesc type; | ||
if (node.Type is not NoTypeName) | ||
{ | ||
type = Utils.GetTypeFromNode(node.Type, context.Driver.Compilation.Module)!; | ||
if (type != value.ResultType) | ||
{ | ||
node.Type.AddError("Type mismatch"); | ||
} | ||
} | ||
else | ||
{ | ||
type = value.ResultType; | ||
} | ||
|
||
var slot = context.Method.Body!.CreateVar(type!, node.Name); | ||
|
||
context.Builder.CreateStore(slot, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module TestSuite; | ||
|
||
func main() -> none { | ||
|
||
let myFlag = true; | ||
} | ||
|
||
private func test(hello: i32, flag: bool) -> i32 | ||
|