Skip to content

Commit

Permalink
self-host(resolver+checker): evalue comptime if
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 25, 2023
1 parent d717536 commit 8e7a0cd
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
11 changes: 7 additions & 4 deletions lib/rivet/src/ast/comptime.ri
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import ../report;

pub struct ComptimeIf {
mut branches: []mut ComptimeIfBranch;
mut branch_idx: ?uint;
has_else: bool;
pos: token.Pos;
mut type: Type;
pub mut branch_idx: ?uint;
pub has_else: bool;
pub pos: token.Pos;
pub mut type: Type;

pub func nodes(self) -> ?[]Node {
if branch_idx := self.branch_idx {
Expand All @@ -33,6 +33,9 @@ pub struct ComptimeIfBranch {

extend Env {
pub func evalue_comptime_if(self, mut comptime_if: ComptimeIf) -> []mut Node {
if branch_idx := comptime_if.branch_idx {
return comptime_if.branches[branch_idx].nodes;
}
for i, branch in comptime_if.branches {
if branch.is_else && comptime_if.branch_idx == none {
comptime_if.branch_idx = i;
Expand Down
10 changes: 8 additions & 2 deletions lib/rivet/src/checker/decls.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ../ast;
Expand Down Expand Up @@ -30,6 +30,12 @@ extend Checker {
old_sym := self.sym;
defer self.sym = old_sym;
match decl {
.ComptimeIf(mut comptime_if) -> {
mut decls := ast.nodes_to_decls(
self.env.evalue_comptime_if(comptime_if)
);
self.check_decls(decls);
},
.Import(import_decl) -> if import_decl.import_list.is_empty() && !import_decl.glob {
if import_decl.has_custom_alias {
self.check_name_case(
Expand Down
7 changes: 7 additions & 0 deletions lib/rivet/src/checker/exprs.ri
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ extend Checker {
return match expr {
.Empty, .Comment -> .Void,
.Type(type) -> type,
.ComptimeIf(mut comptime_if) -> {
mut exprs := ast.nodes_to_exprs(
self.env.evalue_comptime_if(comptime_if)
);
comptime_if.type = self.check_expr(exprs[0]);
comptime_if.type
},
.LoopControl(loop_control) -> {
self.scope_returns = true;
if self.loop_control_tracker != .AnyLoop {
Expand Down
6 changes: 6 additions & 0 deletions lib/rivet/src/checker/stmts.ri
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ extend Checker {

func check_stmt(mut self, mut stmt: ast.Stmt) {
match stmt {
.ComptimeIf(mut comptime_if) -> {
mut stmts := ast.nodes_to_stmts(
self.env.evalue_comptime_if(comptime_if)
);
self.check_stmts(stmts);
},
.VarDecl(mut var_decl) -> self.check_var_decl(
var_decl.lefts, var_decl.right, var_decl.scope, var_decl.pos
),
Expand Down
12 changes: 9 additions & 3 deletions lib/rivet/src/resolver/decls.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ../ast;
Expand All @@ -13,6 +13,12 @@ extend Resolver {
old_self_sym_is_set := self.self_sym_is_set;
match decl {
.Empty(empty_pos) -> report.error("BUG: empty declaration found", empty_pos),
.ComptimeIf(mut comptime_if) -> {
mut ct_decls := ast.nodes_to_decls(
self.env.evalue_comptime_if(comptime_if)
);
self.resolve_decls(ct_decls);
},
.Extern(mut extern_decl) -> self.resolve_decls(extern_decl.decls),
.Alias(mut alias_decl) -> if alias_decl.is_typealias {
_ = self.resolve_type(alias_decl.parent_type);
Expand Down Expand Up @@ -168,7 +174,7 @@ extend Resolver {
report.error(
"{} `{}` has field and {} named `{}`".fmt(
self.self_sym.type_of(), self.self_sym.name, sym.type_of(), field_decl.name
),
),
field_decl.pos
);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/rivet/src/resolver/exprs.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ../ast;
Expand All @@ -10,6 +10,12 @@ extend Resolver {
func resolve_expr(mut self, mut expr: ast.Expr) {
match expr {
.Empty(empty_pos) -> report.error("bug: unexpected empty expression found", empty_pos),
.ComptimeIf(mut comptime_if) -> {
mut exprs := ast.nodes_to_exprs(
self.env.evalue_comptime_if(comptime_if)
);
self.resolve_expr(exprs[0]);
},
.Paren(mut paren) -> self.resolve_expr(paren.expr),
.Type(mut type) -> _ = self.resolve_type(type),
.Ident(mut ident) -> self.resolve_ident(ident),
Expand Down
16 changes: 14 additions & 2 deletions lib/rivet/src/resolver/stmts.ri
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ../ast;
import ../report;

extend Resolver {
func resolve_stmts(mut self, mut stmts: []mut ast.Stmt) {
for mut stmt in stmts {
self.resolve_stmt(stmt);
}
}

func resolve_stmt(mut self, mut stmt: ast.Stmt) {
match stmt {
.Comment -> {},
.Empty(empty_pos) -> report.error("bug: unexpected empty statement found", empty_pos),
.ComptimeIf(mut comptime_if) -> {
mut stmts := ast.nodes_to_stmts(
self.env.evalue_comptime_if(comptime_if)
);
self.resolve_stmts(stmts);
},
.Expr(mut expr) -> self.resolve_expr(expr),
.VarDecl(mut var_stmt) -> {
for mut left in var_stmt.lefts {
Expand Down

0 comments on commit 8e7a0cd

Please sign in to comment.