-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
150 additions
and
32 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
16 changes: 16 additions & 0 deletions
16
Cesium.CodeGen.Tests/CodeGenMethodTests.AmbiguousCallTest.verified.txt
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,16 @@ | ||
System.Int32 <Module>::abs(System.Int32 x) | ||
IL_0000: ldarg x | ||
IL_0004: ret | ||
|
||
System.Void <Module>::exit(System.Int32 x) | ||
IL_0000: ret | ||
|
||
System.Int32 <Module>::main() | ||
Locals: | ||
System.Int32 V_0 | ||
IL_0000: ldc.i4 42 | ||
IL_0005: neg | ||
IL_0006: call System.Int32 <Module>::abs(System.Int32) | ||
IL_000b: stloc V_0 | ||
IL_000f: ldloc V_0 | ||
IL_0013: call System.Void <Module>::exit(System.Int32) |
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
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,73 @@ | ||
using System.Collections.Immutable; | ||
using Cesium.Ast; | ||
using Cesium.CodeGen.Contexts; | ||
using Yoakke.SynKit.C.Syntax; | ||
using Yoakke.SynKit.Lexer; | ||
using Yoakke.SynKit.Text; | ||
using Range = Yoakke.SynKit.Text.Range; | ||
|
||
namespace Cesium.CodeGen.Ir.BlockItems; | ||
|
||
/// <summary> | ||
/// This is a special block item which was constructed in an ambiguous context: it is either a declaration or a function | ||
/// call, depending on the context. | ||
/// | ||
/// It defines an AST of form <code>item1(item2);</code>, where item1 is either a function name or a type, and item2 is | ||
/// either a variable name or an argument name. | ||
/// </summary> | ||
internal class AmbiguousBlockItem : IBlockItem | ||
{ | ||
private readonly string _item1; | ||
private readonly string _item2; | ||
|
||
public AmbiguousBlockItem(Ast.AmbiguousBlockItem item) | ||
{ | ||
(_item1, _item2) = item; | ||
} | ||
|
||
public IBlockItem Lower() => this; | ||
|
||
public void EmitTo(FunctionScope scope) | ||
{ | ||
// Check if this can be a valid variable declaration: | ||
var typeReference = scope.Context.GetTypeReference(_item1); | ||
var isValidVariableDeclaration = typeReference != null; | ||
|
||
// Check if this can be a function call: | ||
var function = scope.Functions.GetValueOrDefault(_item1); | ||
var isValidFunctionCall = function != null; | ||
|
||
if (isValidVariableDeclaration && !isValidFunctionCall) | ||
EmitVariableDeclaration(scope); | ||
else if (!isValidVariableDeclaration && isValidFunctionCall) | ||
EmitFunctionCall(scope); | ||
else if (!isValidVariableDeclaration && !isValidFunctionCall) | ||
throw new NotSupportedException( | ||
$"{_item1}({_item2}) is supposed to be either a variable declaration or a function call," + | ||
" but wasn't resolved to be either."); | ||
else if (isValidVariableDeclaration && isValidFunctionCall) | ||
throw new NotSupportedException( | ||
$"{_item1}({_item2}) is supposed to be either a variable declaration or a function call," + | ||
$" but it's ambiguous which it is, since both a function and a type of name {_item1} exist."); | ||
} | ||
|
||
private void EmitVariableDeclaration(FunctionScope scope) | ||
{ | ||
throw new NotImplementedException("Ambiguous variable declarations aren't supported, yet."); | ||
} | ||
|
||
private void EmitFunctionCall(FunctionScope scope) | ||
{ | ||
CToken CreateFakeToken(string id) => new(new Range(), id, new Range(), id, CTokenType.Identifier); | ||
|
||
var functionNameToken = CreateFakeToken(_item1); | ||
var argumentToken = CreateFakeToken(_item2); | ||
|
||
var functionCallExpression = new Expressions.FunctionCallExpression(new FunctionCallExpression( | ||
new ConstantExpression(functionNameToken), | ||
ImmutableArray.Create<Expression>(new ConstantExpression(argumentToken)) | ||
)); | ||
var realNode = new ExpressionStatement(functionCallExpression); | ||
realNode.Lower().EmitTo(scope); | ||
} | ||
} |
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