Skip to content

Commit

Permalink
Delete compiler/print.jou and move its contents elsewhere (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Jan 10, 2025
1 parent fb65e48 commit 67808e9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 78 deletions.
17 changes: 5 additions & 12 deletions compiler/ast.jou
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import "stdlib/io.jou"
import "stdlib/str.jou"
import "stdlib/mem.jou"
import "./errors_and_warnings.jou"

# TODO: move to stdlib
declare isprint(b: int) -> int
import "./errors_and_warnings.jou"
import "./utils.jou"


enum AstTypeKind:
Expand Down Expand Up @@ -202,15 +201,9 @@ class AstExpression:
def print_with_tree_printer(self, tp: TreePrinter) -> None:
printf("[line %d] ", self->location.lineno)
if self->kind == AstExpressionKind::String:
printf("string \"")
for s = self->string; *s != 0; s++:
if isprint(*s) != 0:
putchar(*s)
elif *s == '\n':
printf("\\n")
else:
printf("\\x%02x", *s)
printf("\"\n")
printf("string ")
print_string(self->string, strlen(self->string))
printf("\n")
elif self->kind == AstExpressionKind::Short:
printf("short %hd\n", self->short_value)
elif self->kind == AstExpressionKind::Int:
Expand Down
4 changes: 2 additions & 2 deletions compiler/cf_graph.jou
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import "stdlib/mem.jou"
import "stdlib/io.jou"

import "./errors_and_warnings.jou"
import "./utils.jou"
import "./structs.jou"
import "./typecheck/common.jou"
import "./print.jou"
import "./types.jou"


Expand Down Expand Up @@ -128,7 +128,7 @@ class CfInstruction:
self->operands[0]->print()
printf(" from 64-bit integer to pointer")
elif self->kind == CfInstructionKind::Constant:
print_constant(&self->constant)
self->constant.print()
elif self->kind == CfInstructionKind::SpecialConstant:
printf("special constant \"%s\"", self->scname)
elif self->kind == CfInstructionKind::StringArray:
Expand Down
16 changes: 15 additions & 1 deletion compiler/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import "./build_cf_graph.jou"
import "./evaluate.jou"
import "./run.jou"
import "./codegen.jou"
import "./print.jou"
import "./llvm.jou"
import "./output.jou"
import "./typecheck/common.jou"
Expand Down Expand Up @@ -180,6 +179,21 @@ def statement_conflicts_with_an_import(stmt: AstStatement*, importsym: ExportSym
assert False


def print_llvm_ir(module: LLVMModule*, is_optimized: bool) -> None:
if is_optimized:
opt_or_unopt = "Optimized"
else:
opt_or_unopt = "Unoptimized"

len = 0L
filename = LLVMGetSourceFileName(module, &len)
printf("===== %s LLVM IR for file \"%.*s\" =====\n", opt_or_unopt, len as int, filename)

s = LLVMPrintModuleToString(module)
puts(s)
LLVMDisposeMessage(s)


class FileState:
path: byte* # owned
ast: AstFile
Expand Down
63 changes: 0 additions & 63 deletions compiler/print.jou

This file was deleted.

34 changes: 34 additions & 0 deletions compiler/structs.jou
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# TODO: delete this file, merge into others

import "stdlib/str.jou"
import "stdlib/io.jou"
import "stdlib/mem.jou"

import "./utils.jou"
import "./types.jou"


Expand Down Expand Up @@ -49,6 +51,38 @@ class Constant:
boolean: bool
enum_member: EnumMemberConstant

def print(self) -> None:
if self->kind == ConstantKind::EnumMember:
printf("enum member %d of %s", self->enum_member.memberidx, self->enum_member.enumtype->name)
elif self->kind == ConstantKind::Bool:
if self->boolean:
printf("True")
else:
printf("False")
elif self->kind == ConstantKind::Float:
printf("float %s", self->double_or_float_text)
elif self->kind == ConstantKind::Double:
printf("double %s", self->double_or_float_text)
elif self->kind == ConstantKind::Integer:
if self->integer.is_signed:
signed_or_unsigned = "signed"
else:
signed_or_unsigned = "unsigned"
printf(
"%lld (%d-bit %s)",
self->integer.value,
self->integer.size_in_bits,
signed_or_unsigned,
)
elif self->kind == ConstantKind::Null:
printf("NULL")
elif self->kind == ConstantKind::String:
printf("string ")
print_string(self->str, strlen(self->str))
printf("\n")
else:
assert False

def free(self) -> None:
if self->kind == ConstantKind::String:
free(self->str)
Expand Down
14 changes: 14 additions & 0 deletions compiler/utils.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "stdlib/io.jou"


def print_string(s: byte*, len: long) -> None:
putchar('"')
for i = 0; i < len; i++:
if 32 <= s[i] and s[i] <= 126:
# printable ascii character
putchar(s[i])
elif s[i] == '\n':
printf("\\n")
else:
printf("\\x%02x", s[i]) # TODO: \x is not yet recognized by the tokenizer
putchar('"')

0 comments on commit 67808e9

Please sign in to comment.