Skip to content

Commit

Permalink
Fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Mar 20, 2024
1 parent 550e242 commit d10b368
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/codegen.pr
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def emit_switch(fp: File, insn: &compiler::Insn) {
fprint(fp, "\t]")
}

def emit(fp: File, insn: &compiler::Insn) {
export def emit(fp: File, insn: &compiler::Insn) {
fprint(fp, "\t")
switch (@insn).kind !int {
case compiler::InsnKind::FNEG
Expand Down
1 change: 1 addition & 0 deletions src/compiler.pr
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ def push_alloca(insn: &Insn, state: &State, no_yield_capture: bool = false) {
let ret = insn.value.alloca.ret
let index = current_function.all_locals.size !int + 1
push_local_var(ret.name, ret.tpe, current_function)
ret.tpe = pointer(ret.tpe)

let gep = make_insn(InsnKind::GETELEMENTPTR)
gep.value.gep = [
Expand Down
1 change: 1 addition & 0 deletions src/consteval.pr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export let const_module = toolchain::make_module(
node = null,
scpe = scope::enter_function_scope(builtins::builtins, null),
)
const_module.delayed_compile = vector::make(type &parser::Node)

export var is_static = false
// This is for compiling expressions
Expand Down
19 changes: 12 additions & 7 deletions src/eval.pr
Original file line number Diff line number Diff line change
Expand Up @@ -991,14 +991,16 @@ def eval_Call(insn: &compiler::Insn, state: &State) {

push_stack_frame(stack_frame, state)

/*print(function.name, "\n")
print("==========================\n")
var block = function.block
while block {
codegen::emit_block(std::stdout(), block)
block = block.next
/*if toolchain::trace_consteval {
print(function.name, "\n")
print("==========================\n")
var block = function.block
while block {
codegen::emit_block(std::stdout(), block)
block = block.next
}
}*/

eval(function.block, state)

if ret.tpe {
Expand Down Expand Up @@ -1101,6 +1103,9 @@ def eval_Inttoptr(insn: &compiler::Insn, state: &State) {
}

def eval_insn(insn: &compiler::Insn, state: &State) {
if toolchain::trace_consteval {
codegen::emit(std::stdout(), insn)
}
switch insn.kind !int {
case compiler::InsnKind::INSERTVALUE
eval_InsertValue(insn, state)
Expand Down
3 changes: 3 additions & 0 deletions src/main.pr
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ let options = [
.set_metavar("NAME")),
(option(*toolchain::no_stdlib, "--no-stdlib")
.set_help("Compile without importing std")),
(option(*toolchain::trace_consteval, "--trace-consteval")
.set_help("Trace code generated for consteval")),
(option(*filename, "compile")
.set_help("File to compile")
.set_metavar("FILE"))
Expand All @@ -111,6 +113,7 @@ if language_server {
}

if not filename {
toolchain::is_in_repl = true
repl::run()
exit(0)
}
Expand Down
6 changes: 6 additions & 0 deletions src/repl.pr
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ def execute(source: Str) {
map::remove(module.result.functions, "const::__main__::([[char]])")
compiler::compile(cstate, true, true)

// Compile delayed functions
for var n in consteval::const_module.delayed_compile {
compiler::create_function(n, n.tpe, n.value.def_.body, n.inner_scope, null, consteval::const_module.compiler_state)
}
consteval::const_module.delayed_compile = vector::make(type &parser::Node)

var estate = [
cstate = toolchain::types_state,
stack = vector::make(type &eval::StackFrame)
Expand Down
5 changes: 5 additions & 0 deletions src/toolchain.pr
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ export var link_libraries = vector::make(Str)
export var link_flags = vector::make(Str)
export var rdynamic = false

export var trace_consteval = false
export var dependency_graph = false
export var no_incremental = false
// This is so that new entries don't depend on modules that are already collected
export var no_dependency_tracking = false
export var is_shared = false
export var no_stdlib = false
export var is_in_repl = false

let include: Str = util::exe_folder() + "/../include"
let stdlib: Str = util::exe_folder() + "/../std"
Expand Down Expand Up @@ -240,6 +242,9 @@ export type Module = struct {

// We keep track of interface implementations for this module
interfaces: &Map(&typechecking::Type, &Map(&typechecking::Type, &typechecking::Type))
// Only used by const_module, this contains nodes of functions which still need to be compiled
// This is needed for the repl because we only compile the current module multiple times
delayed_compile: &Vector(&parser::Node)
}

export def get_impl(module: &Module, intf: &typechecking::Type, wrap: &typechecking::Type) -> &typechecking::Type {
Expand Down
7 changes: 7 additions & 0 deletions src/typechecking.pr
Original file line number Diff line number Diff line change
Expand Up @@ -6309,6 +6309,13 @@ export def walk_Def_with_type_argument(node: &parser::Node, parameter_t: &Vector

// This makes sure that the function is walked by the compiler
state.module.node.body.push(node)

// Extra state for the repl
if toolchain::is_in_repl {
compiler::predeclare_function(function)
consteval::const_module.result.functions(node.tpe.type_name) = function
consteval::const_module.delayed_compile.push(node)
}

if not in_context {
super_scope.parent = builtins::builtins
Expand Down

0 comments on commit d10b368

Please sign in to comment.