Skip to content

Commit

Permalink
Added structType for parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaceby committed Apr 14, 2024
1 parent 083f079 commit 48f4d66
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
25 changes: 5 additions & 20 deletions examples/test.br
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
// main.br

fn combine <T> (a: T, b: T) -> T {
return a + b;
struct Combinable<T> {
data: T;
}

struct Circle {
radius: number;

static fn new (r: number) -> Circle {
return Circle {
radius: r,
};
}

fn setRadius (r: number) -> Circle {
self.radius = r;
}
}

let message = combine <string>("Hello ", "world");
message = "Hello ";

const foo = message + "world";
const c = Combinable<number>{
data: 120.2,
};
7 changes: 7 additions & 0 deletions src/ast/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package ast

type StructType struct {
GenericList []string
StructName string
}

func (t StructType) _type() {}

type SymbolType struct {
Value string
}
Expand Down
3 changes: 1 addition & 2 deletions src/parser/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ func parse_struct_instantiation(p *parser, left ast.Expr, bp binding_power) ast.

func parse_generic_list_instantiation(p *parser, left ast.Expr, bp binding_power) ast.Expr {
var genericLists = []ast.Type{}

p.expect(lexer.LESS)

for p.hasTokens() && p.currentTokenKind() != lexer.GREATER {
Expand All @@ -240,7 +239,7 @@ func parse_generic_list_instantiation(p *parser, left ast.Expr, bp binding_power

// Handle struct instantiation -> Struct <T> {}
if p.currentTokenKind() == lexer.OPEN_CURLY {
structNode := ast.ExpectExpr[ast.StructInstantiationExpr](parse_call_expr(p, left, call))
structNode := ast.ExpectExpr[ast.StructInstantiationExpr](parse_struct_instantiation(p, left, call))
structNode.Generics = genericLists

return structNode
Expand Down
23 changes: 23 additions & 0 deletions src/parser/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/tlaceby/bedrock/src/ast"
"github.com/tlaceby/bedrock/src/helpers"
"github.com/tlaceby/bedrock/src/lexer"
)

Expand Down Expand Up @@ -32,6 +33,8 @@ func createTypeTokenLookups() {
type_nud(lexer.IDENTIFIER, parse_symbol_type) // T
type_nud(lexer.OPEN_BRACKET, parse_list_type) // []T
type_nud(lexer.FN, parse_fn_type) // fn (...[]Type) -> Type

type_led(lexer.LESS, primary, parse_generic_type) // StructName<T, ..>
}

func parse_type(p *parser, bp binding_power) ast.Type {
Expand All @@ -58,6 +61,26 @@ func parse_type(p *parser, bp binding_power) ast.Type {
return left
}

func parse_generic_type(p *parser, left ast.Type, bp binding_power) ast.Type {
p.expect(lexer.LESS)
generics := make([]string, 0)

for p.hasTokens() && p.currentTokenKind() != lexer.GREATER {
genericName := p.expect(lexer.IDENTIFIER).Value
generics = append(generics, genericName)

if p.currentTokenKind() != lexer.GREATER {
p.expect(lexer.COMMA)
}
}

p.expect(lexer.GREATER)
return ast.StructType{
GenericList: generics,
StructName: helpers.ExpectType[ast.SymbolType](left).Value,
}
}

func parse_symbol_type(p *parser) ast.Type {
return ast.SymbolType{
Value: p.advance().Value,
Expand Down

0 comments on commit 48f4d66

Please sign in to comment.