Skip to content

Commit

Permalink
base type
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwanjacobi committed Mar 9, 2024
1 parent 8edd1e3 commit 62889e1
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src2/Maja/Maja.Compiler/IR/IrBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,14 @@ private IrDeclarationType DeclarationType(TypeDeclarationSyntax syntax)
_diagnostics.TypeAlreadyDeclared(syntax.Location, syntax.Name.Text);
}

var baseType = Type(syntax.BaseType);

// TODO: inline declared types
var locality = CurrentScope.IsExport(name) || syntax.IsPublic
? IrLocality.Public
: IrLocality.None;

return new IrDeclarationType(syntax, symbol, typeParameters, enums, fields, rules, typeScope, locality);
return new IrDeclarationType(syntax, symbol, typeParameters, enums, fields, rules, baseType, typeScope, locality);
}

private IEnumerable<IrTypeMemberEnum> TypeMemberEnums(TypeMemberListSyntax<MemberEnumSyntax>? syntax)
Expand Down
5 changes: 4 additions & 1 deletion src2/Maja/Maja.Compiler/IR/IrDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ public IEnumerable<T> GetDescendentsOfType<T>() where T : IrNode
internal sealed class IrDeclarationType : IrDeclaration, IrContainer
{
public IrDeclarationType(TypeDeclarationSyntax syntax, TypeSymbol symbol, IEnumerable<IrTypeParameter> typeParameters,
IEnumerable<IrTypeMemberEnum> enums, IEnumerable<IrTypeMemberField> fields, IEnumerable<IrTypeMemberRule> rules, IrTypeScope scope, IrLocality locality)
IEnumerable<IrTypeMemberEnum> enums, IEnumerable<IrTypeMemberField> fields, IEnumerable<IrTypeMemberRule> rules,
IrType? baseType, IrTypeScope scope, IrLocality locality)
: base(syntax, locality)
{
Symbol = symbol;
Scope = scope;
BaseType = baseType;
TypeParameters = typeParameters.ToImmutableArray();
Enums = enums.ToImmutableArray();
Fields = fields.ToImmutableArray();
Expand All @@ -102,6 +104,7 @@ public IrDeclarationType(TypeDeclarationSyntax syntax, TypeSymbol symbol, IEnume

public TypeSymbol Symbol { get; }
public IrTypeScope Scope { get; }
public IrType? BaseType { get; }
public ImmutableArray<IrTypeParameter> TypeParameters { get; }
public ImmutableArray<IrTypeMemberEnum> Enums { get; }
public ImmutableArray<IrTypeMemberField> Fields { get; }
Expand Down
6 changes: 4 additions & 2 deletions src2/Maja/Maja.Compiler/IR/IrRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,16 @@ protected virtual IrDeclarationType RewriteDeclarationType(IrDeclarationType typ
var enums = RewriteEnums(type.Enums);
var fields = RewriteFields(type.Fields);
var rules = RewriteRules(type.Rules);
var baseType = RewriteType(type.BaseType);

if (typeParams == type.TypeParameters &&
enums == type.Enums &&
fields == type.Fields &&
rules == type.Rules)
rules == type.Rules &&
baseType == type.BaseType)
return type;

return new IrDeclarationType(type.Syntax, type.Symbol, typeParams, enums, fields, rules, type.Scope, type.Locality);
return new IrDeclarationType(type.Syntax, type.Symbol, typeParams, enums, fields, rules, baseType, type.Scope, type.Locality);
}

protected virtual ImmutableArray<IrTypeMemberEnum> RewriteEnums(ImmutableArray<IrTypeMemberEnum> memberEnums)
Expand Down
6 changes: 6 additions & 0 deletions src2/Maja/Maja.Compiler/Syntax/TypeDeclarationSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public override SyntaxKind SyntaxKind
public NameSyntax Name
=> ChildNodes.OfType<NameSyntax>().Single();

/// <summary>
/// Optional base type.
/// </summary>
public TypeSyntax? BaseType
=> ChildNodes.OfType<TypeSyntax>().SingleOrDefault();

/// <summary>
/// The type parameters, if any.
/// </summary>
Expand Down
11 changes: 6 additions & 5 deletions src2/Maja/Maja.Compiler/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Maja Compiler
# Maja Compiler

## TODO

Expand All @@ -9,11 +9,12 @@ Started. Have ErrorToken with description.
- [ ] Syntax: Assignment to member access expression (l-value) (now nameIdentifier)
- [ ] Syntax: serialze Syntax model into Maja code. Should be the exact same as source.
- [ ] Indents (dedent) are not working correct with multiple indents (`SyntaxWriter`).
- [ ] Type: base type (enum/struct/value)
- [ ] Type: Generics
- [ ] Type: base type (enum/struct/value).
- [ ] Resolve base types of derived types.
- [ ] Type: Generics (type inference)
- [ ] Type: Template
- [ ] Type: Comp-parameter
- [ ] Function: Generics
- [ ] Function: Generics (type inference)
- [ ] Function: Template
- [ ] Function: Comp-parameter
- [ ] Value (custom) Types (/w rules)
Expand Down Expand Up @@ -76,4 +77,4 @@ Only necessary when tree is allowed to change.

## Notes

- CodeBlock is a statement or an expression?
- Is CodeBlock a statement or an expression?
33 changes: 33 additions & 0 deletions src2/Maja/Maja.UnitTests/Compiler/IR/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ public void TypeDeclareFields()
type.Fields[1].Type.Symbol.Should().Be(TypeSymbol.Str);
}

[Fact]
public void TypeDeclareFields_BaseType()
{
const string code =
"BaseType" + Tokens.Eol +
Tokens.Indent1 + "fld1: U8" + Tokens.Eol +
"MyType : BaseType" + Tokens.Eol +
Tokens.Indent1 + "fld2: Str" + Tokens.Eol
;

var program = Ir.Build(code);
program.Root.Should().NotBeNull();
program.Root.Declarations.Should().HaveCount(2);
var baseType = program.Root.Declarations[0].As<IrDeclarationType>();
var symbol = baseType.Symbol.As<DeclaredTypeSymbol>();
symbol.Name.Value.Should().Be("Basetype");
symbol.Fields.Should().HaveCount(1);
baseType.Fields.Should().HaveCount(1);
baseType.Fields[0].DefaultValue.Should().BeNull();
baseType.Fields[0].Symbol.Name.Value.Should().Be("fld1");
baseType.Fields[0].Type.Symbol.Should().Be(TypeSymbol.U8);

var type = program.Root.Declarations[1].As<IrDeclarationType>();
type.BaseType!.Symbol.Name.Value.Should().Be("Basetype");
symbol = type.Symbol.As<DeclaredTypeSymbol>();
symbol.Name.Value.Should().Be("Mytype");
symbol.Fields.Should().HaveCount(1);
type.Fields.Should().HaveCount(1);
type.Fields[0].DefaultValue.Should().BeNull();
type.Fields[0].Symbol.Name.Value.Should().Be("fld2");
type.Fields[0].Type.Symbol.Should().Be(TypeSymbol.Str);
}

[Fact]
public void TypeDeclareFields_Generics()
{
Expand Down
18 changes: 18 additions & 0 deletions src2/Maja/Maja.UnitTests/Compiler/Syntax/TypeSyntaxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public void TypeDeclareStruct()
Syntax.RoundTrip(code, _output);
}

[Fact]
public void TypeDeclareStruct_Derived()
{
const string code =
"Type : BaseType" + Tokens.Eol +
Tokens.Indent1 + "fld1: U8" + Tokens.Eol
;

var result = Syntax.Parse(code);
result.Members.Should().HaveCount(1);
var t = result.Members.First().As<TypeDeclarationSyntax>();
t.Name.Text.Should().Be("Type");
t.BaseType.Should().NotBeNull();
t.BaseType!.Name.Text.Should().Be("BaseType");

Syntax.RoundTrip(code, _output);
}

[Fact]
public void TypeDeclareStruct_Generics()
{
Expand Down
5 changes: 3 additions & 2 deletions src2/Maja/Maja.UnitTests/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ Keeping track of the progress of Maja features.

|Feature | |Grammar|Syntax|Ir|Checks|Lower|Repl|Emit|Runtime|Description|
|-------------|-----|:-:|:-:|:-:|:-:|:-:|:-:|:-:|---|---------------|
| struct | | x | x | x | | | x | x | | `MyType -> fld: U8` |
| struct | flds | x | x | x | | | x | x | | `MyType -> fld: U8` |
| | enum | x | x | | | | | | | `MyType -> fld: U8 -> Opt1, Opt2` |
| | init | x | x | x | | | x | x | | `MyType -> fld = 42` |
| generics | | x | x | x | | | | | | `MyType<T> -> fld: T` |
| template | | x | x | | | | | | | `MyType<#T> -> fld: T` |
| enum | | x | x | x | | | | x | | `MyType -> opt1, opt2` |
| custom | | | | | | | | | | `MyType: U8` |
| rule | | | | | | | | | | `MyType -> #fld1 > 0` |
| struct | init | x | x | x | | | x | x | | `MyType -> fld = 42` |
| baseType | flds | x | x | x | | | | | | `MyType : BaseType` |

## Expressions

Expand Down

0 comments on commit 62889e1

Please sign in to comment.