-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from NoiseStudio/feature/268/add-constructors…
…-to-compiled-default-shader Add constructors to compiled default shader
- Loading branch information
Showing
20 changed files
with
427 additions
and
81 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
NoiseEngine.Tests/Nesl/CompilerTools/Parsing/Methods/Assignment.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,43 @@ | ||
using NoiseEngine.Tests.Environments; | ||
|
||
namespace NoiseEngine.Tests.Nesl.CompilerTools.Parsing.Methods; | ||
|
||
public class Assignment : NeslParsingTestEnvironment { | ||
|
||
[Fact] | ||
public void CompactValueFromVariable() { | ||
CompileSingle(@" | ||
void Main(f32 a, f32v3 b) { | ||
a = b.Z; | ||
} | ||
"); | ||
} | ||
|
||
[Fact] | ||
public void CompactValueFromVariableConstructor() { | ||
CompileSingle(@" | ||
struct Foo { | ||
Foo(f32 a, f32v3 b) { | ||
a = b.Z; | ||
} | ||
} | ||
"); | ||
} | ||
|
||
[Fact] | ||
public void CompactValueFromField() { | ||
CompileSingle(@" | ||
struct Foo { | ||
f32 a; | ||
f32v4 b; | ||
void Main(f32 a) { | ||
a = b.Y; | ||
} | ||
} | ||
"); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
NoiseEngine.Tests/Nesl/CompilerTools/Parsing/Methods/GenericType.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,20 @@ | ||
using NoiseEngine.Tests.Environments; | ||
|
||
namespace NoiseEngine.Tests.Nesl.CompilerTools.Parsing.Methods; | ||
|
||
public class GenericType : NeslParsingTestEnvironment { | ||
|
||
[Fact] | ||
public void Generic() { | ||
CompileSingle(@" | ||
struct Foo<T> { | ||
T Method(T t) { | ||
return t; | ||
} | ||
} | ||
"); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
NoiseEngine.Tests/Nesl/CompilerTools/Parsing/Structs/Constructors.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,37 @@ | ||
using NoiseEngine.Tests.Environments; | ||
|
||
namespace NoiseEngine.Tests.Nesl.CompilerTools.Parsing.Structs; | ||
|
||
public class Constructors : NeslParsingTestEnvironment { | ||
|
||
[Fact] | ||
public void WithoutParameters() { | ||
CompileSingle(@" | ||
struct Foo { | ||
public u32 X; | ||
public Foo() { | ||
X = 42; | ||
} | ||
} | ||
"); | ||
} | ||
|
||
[Fact] | ||
public void WithParameters() { | ||
CompileSingle(@" | ||
struct Foo { | ||
public f32 X; | ||
public Foo(f32 x) { | ||
X = x; | ||
} | ||
} | ||
"); | ||
} | ||
|
||
} |
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
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
51 changes: 48 additions & 3 deletions
51
NoiseEngine/Nesl/CompilerTools/Parsing/Constructors/ValueData.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 |
---|---|---|
@@ -1,7 +1,52 @@ | ||
namespace NoiseEngine.Nesl.CompilerTools.Parsing.Constructors; | ||
using NoiseEngine.Nesl.CompilerTools.Parsing.Tokens; | ||
using NoiseEngine.Nesl.Emit; | ||
|
||
internal readonly record struct ValueData(NeslType Type, uint Id) { | ||
namespace NoiseEngine.Nesl.CompilerTools.Parsing.Constructors; | ||
|
||
public static ValueData Invalid => new ValueData(null!, uint.MaxValue); | ||
internal readonly record struct ValueData(NeslType Type, uint Id, object? AdditionalData) { | ||
|
||
public static ValueData Invalid => new ValueData(null!, uint.MaxValue, null); | ||
|
||
public bool IsInvalid => this == Invalid; | ||
|
||
public ValueData(NeslType type, uint id) : this(type, id, null) { | ||
} | ||
|
||
public ValueData LoadConst(Parser parser, NeslType expected) { | ||
const string Start = "System::System."; | ||
|
||
if (AdditionalData is not ConstValueToken constValue) | ||
return this; | ||
|
||
IlGenerator il = parser.CurrentMethod.IlGenerator; | ||
uint id = uint.MaxValue; | ||
CompilationError error = default; | ||
|
||
switch (expected.FullNameWithAssembly) { | ||
case Start + "Float32": | ||
if (constValue.ToFloat32(out float a, out error)) { | ||
id = il.GetNextVariableId(); | ||
il.Emit(OpCode.DefVariable, expected); | ||
il.Emit(OpCode.LoadFloat32, id, a); | ||
} | ||
break; | ||
case Start + "UInt32": | ||
if (constValue.ToUInt32(out uint b, out error)) { | ||
id = il.GetNextVariableId(); | ||
il.Emit(OpCode.DefVariable, expected); | ||
il.Emit(OpCode.LoadUInt32, id, b); | ||
} | ||
break; | ||
default: | ||
parser.Throw(new CompilationError( | ||
constValue.Pointer, CompilationErrorType.ImplicitCastOperatorNotFound, constValue | ||
)); | ||
break; | ||
} | ||
|
||
if (id == uint.MaxValue) | ||
parser.Throw(error); | ||
return new ValueData(expected, id); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
NoiseEngine/Nesl/CompilerTools/Parsing/DefaultConstructorHelper.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,23 @@ | ||
using NoiseEngine.Nesl.Emit; | ||
using System.Diagnostics; | ||
|
||
namespace NoiseEngine.Nesl.CompilerTools.Parsing; | ||
|
||
internal static class DefaultConstructorHelper { | ||
|
||
public static void AppendHeader(NeslMethodBuilder method) { | ||
NeslType type = method.ReturnType ?? throw new UnreachableException(); | ||
IlGenerator il = method.IlGenerator; | ||
|
||
il.GetNextVariableId(); | ||
il.Emit(OpCode.DefVariable, type); | ||
} | ||
|
||
public static void AppendFooter(NeslMethodBuilder method) { | ||
NeslType type = method.ReturnType ?? throw new UnreachableException(); | ||
IlGenerator il = method.IlGenerator; | ||
|
||
il.Emit(OpCode.ReturnValue, (uint)type.Fields.Count); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
NoiseEngine/Nesl/CompilerTools/Parsing/Expressions/AssignmentDeclaration.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,73 @@ | ||
using NoiseEngine.Nesl.CompilerTools.Parsing.Constructors; | ||
using NoiseEngine.Nesl.CompilerTools.Parsing.Tokens; | ||
using NoiseEngine.Nesl.Emit; | ||
using System; | ||
|
||
namespace NoiseEngine.Nesl.CompilerTools.Parsing.Expressions; | ||
|
||
internal class AssignmentDeclaration : ParserExpressionContainer { | ||
|
||
public AssignmentDeclaration(Parser parser) : base(parser) { | ||
} | ||
|
||
[ParserExpression(ParserStep.Method)] | ||
[ParserExpressionParameter(ParserTokenType.TypeIdentifier)] | ||
[ParserExpressionParameter(ParserTokenType.Operator)] | ||
[ParserExpressionParameter(ParserTokenType.Value)] | ||
[ParserExpressionTokenType(ParserTokenType.Semicolon)] | ||
public void Define(TypeIdentifierToken identifier, OperatorToken op, ValueToken value) { | ||
bool successful = true; | ||
if (identifier.GenericTokens.Count != 0) { | ||
Parser.Throw(new CompilationError( | ||
identifier.GenericTokens[0].Pointer, CompilationErrorType.AssigmentGenericNotAllowed, identifier | ||
)); | ||
successful = false; | ||
} | ||
if (!op.IsAssigment) { | ||
Parser.Throw(new CompilationError(op.Pointer, CompilationErrorType.ExpectedAssigmentOperator, op)); | ||
successful = false; | ||
} | ||
|
||
int index = identifier.Identifier.IndexOf('.'); | ||
if (index != -1) | ||
throw new NotImplementedException(); | ||
|
||
VariableData? variable = Parser.GetVariable(identifier.Identifier); | ||
bool isField = false; | ||
|
||
// Check fields. | ||
if (variable is null) { | ||
uint i = 0; | ||
foreach (NeslField field in Parser.CurrentMethod.Type.Fields) { | ||
if (field.Name == identifier.Identifier) { | ||
variable = new VariableData(field.FieldType, field.Name, i); | ||
isField = true; | ||
break; | ||
} | ||
i++; | ||
} | ||
} | ||
|
||
// TODO: Add properties. | ||
|
||
if (variable is null) { | ||
Parser.Throw(new CompilationError( | ||
identifier.Pointer, CompilationErrorType.VariableOrFieldOrPropertyNotFound, identifier.Identifier | ||
)); | ||
successful = false; | ||
} | ||
|
||
ValueData valueData = ValueConstructor.Construct(value, Parser); | ||
if (!successful || valueData.IsInvalid) | ||
return; | ||
valueData = valueData.LoadConst(Parser, variable!.Value.Type); | ||
|
||
IlGenerator il = Parser.CurrentMethod.IlGenerator; | ||
if (!isField) { | ||
il.Emit(OpCode.Load, variable!.Value.Id, valueData.Id); | ||
} else { | ||
il.Emit(OpCode.SetField, (uint)Parser.CurrentMethod.Type.Fields.Count, variable!.Value.Id, valueData.Id); | ||
} | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
NoiseEngine/Nesl/CompilerTools/Parsing/Expressions/ConstructorDeclaration.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,26 @@ | ||
using NoiseEngine.Nesl.CompilerTools.Parsing.Tokens; | ||
|
||
namespace NoiseEngine.Nesl.CompilerTools.Parsing.Expressions; | ||
|
||
internal class ConstructorDeclaration : ParserExpressionContainer { | ||
|
||
public ConstructorDeclaration(Parser parser) : base(parser) { | ||
} | ||
|
||
[ParserExpression(ParserStep.Type)] | ||
[ParserExpressionParameter(ParserTokenType.Attributes)] | ||
[ParserExpressionParameter(ParserTokenType.AccessModifiers)] | ||
[ParserExpressionParameter(ParserTokenType.Name)] | ||
[ParserExpressionParameter(ParserTokenType.RoundBrackets)] | ||
[ParserExpressionParameter(ParserTokenType.CurlyBrackets)] | ||
public void Define( | ||
AttributesToken attributes, AccessModifiersToken accessModifiers, NameToken name, RoundBracketsToken parameters, | ||
CurlyBracketsToken codeBlock | ||
) { | ||
Parser.DefineMethod(new MethodDefinitionData( | ||
null, name with { Name = NeslOperators.Constructor }, parameters.Buffer, codeBlock.Buffer, | ||
attributes.Compile(Parser, AttributeTargets.Method) | ||
)); | ||
} | ||
|
||
} |
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
Oops, something went wrong.