Skip to content

Commit fd7edd2

Browse files
committed
add inline return (not sure if it should be used)
1 parent 7aed0ec commit fd7edd2

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

Cmc/Core/Environment.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class Environment
1616
/// </summary>
1717
public static Environment Galaxy;
1818

19+
private static readonly MetaData BuiltIn = new MetaData(-1, "[built-in]");
20+
1921
public static Environment SolarSystem;
2022

2123
[NotNull] public readonly IList<Declaration> Declarations = new List<Declaration>();
@@ -40,17 +42,17 @@ from builtinType in new[]
4042
PrimaryType.BoolType
4143
}
4244
select new TypeDeclaration(
43-
MetaData.BuiltIn,
45+
BuiltIn,
4446
builtinType,
45-
new PrimaryType(MetaData.BuiltIn, builtinType)))
47+
new PrimaryType(BuiltIn, builtinType)))
4648
Galaxy.Declarations.Add(typeDeclaration);
47-
var puts = new ExternDeclaration(MetaData.BuiltIn, "print", null,
48-
new LambdaType(MetaData.BuiltIn,
49+
var puts = new ExternDeclaration(BuiltIn, "print", null,
50+
new LambdaType(BuiltIn,
4951
new List<Type>
5052
{
51-
new PrimaryType(MetaData.BuiltIn, PrimaryType.StringType)
53+
new PrimaryType(BuiltIn, PrimaryType.StringType)
5254
},
53-
new PrimaryType(MetaData.BuiltIn, PrimaryType.NullType)));
55+
new PrimaryType(BuiltIn, PrimaryType.NullType)));
5456
puts.SurroundWith(Galaxy);
5557
SolarSystem.Declarations.Add(puts);
5658
}

Cmc/Core/MetaData.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ public string GetErrorHeader() =>
2323

2424
private static int _count;
2525
public static readonly MetaData Empty = new MetaData(_count++, "Unknown");
26-
public static readonly MetaData BuiltIn = new MetaData(-1, "[built-in]");
2726
}
2827
}

Cmc/Stmt/Statement.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,23 @@ public override void SurroundWith(Environment environment)
8787
new ReturnStatement(MetaData, new VariableExpression(MetaData, variableName), _labelName));
8888
}
8989

90-
public void InlineThis()
90+
/// <summary>
91+
/// make this an inlined return statement
92+
/// </summary>
93+
/// <param name="returnValueStorer">the variable used to store the return value</param>
94+
public void InlineThis(VariableExpression returnValueStorer)
9195
{
96+
if (null != ConvertedStatementList)
97+
{
98+
var varDecl = ConvertedStatementList.Statements.First();
99+
var varExpr = (VariableExpression) ((ReturnStatement) ConvertedStatementList.Statements.Last()).Expression;
100+
ConvertedStatementList = new StatementList(MetaData,
101+
varDecl,
102+
new AssignmentStatement(MetaData, returnValueStorer, varExpr));
103+
}
104+
else
105+
ConvertedStatementList = new StatementList(MetaData,
106+
new AssignmentStatement(MetaData, returnValueStorer, Expression));
92107
}
93108

94109
public override IEnumerable<string> Dump() => new[]

0 commit comments

Comments
 (0)