Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Princess-org/Princess
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed May 6, 2024
2 parents 907831f + b1e300c commit 5af28c1
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/codegen.pr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import builtins
def type_to_str(tpe: &typechecking::Type) -> Str {
if not tpe { return "void" }
if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
}
var ret: StringBuffer = ""
switch tpe.kind !int {
Expand Down
28 changes: 14 additions & 14 deletions src/compiler.pr
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ def take_snapshot(function: &Function) {

export type Function = struct {
is_global: bool
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
imported: bool
dllimport: bool
dllexport: bool
Expand Down Expand Up @@ -518,9 +518,9 @@ export type Function = struct {
// Closures
is_closure: bool
state: &typechecking::Type
scope: weak_ref(scope::Scope)
inner_scope: weak_ref(scope::Scope)
captures: &Vector(weak_ref(scope::Value))
scope: weak &scope::Scope
inner_scope: weak &scope::Scope
captures: &Vector(weak &scope::Value)

is_compiled: bool
is_typechecked: bool
Expand Down Expand Up @@ -567,7 +567,7 @@ type LoopState = struct {
}

export type State = struct {
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
global_counter: int
meta_counter: int
local_counter: int
Expand All @@ -584,7 +584,7 @@ export type State = struct {
// Used by eval
globals: &SMap(*)
mem: &arena::Arena
scope: weak_ref(scope::Scope)
scope: weak &scope::Scope
// Destructor function
finalizer: &Function
consteval: bool
Expand Down Expand Up @@ -1111,7 +1111,7 @@ def import_structure(tpe: &typechecking::Type, module: &toolchain::Module) {
export def import_structures(tpe: &typechecking::Type, module: &toolchain::Module) {
if not tpe { return }
if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
}
switch tpe.kind !int {
case typechecking::TypeKind::STRUCT..=typechecking::TypeKind::UNION
Expand Down Expand Up @@ -3079,7 +3079,7 @@ def walk_Call(node: &parser::Node, state: &State) -> Value {
insert_copy_constructor(ret, expr, loc, state)
expr = state.load(expr.tpe, ret, loc)
}
// TODO Does this need to do something for weak references?
// TODO Does this need to do something for wk references?
if typechecking::is_ref(last_np.tpe) and not typechecking::is_ref(n.tpe) {
create_type(reference(n.tpe), state.module)
// We need to increase the ref count and add the destructor call
Expand Down Expand Up @@ -4046,7 +4046,7 @@ def walk_MemberAccess_struct(node: &parser::Node, tpe: &typechecking::Type, memb

var member_type = member.tpe
if member.tpe.kind == typechecking::TypeKind::BOX {
member_type = member.tpe.weak
member_type = member.tpe.wk
}

if tpe.kind == typechecking::TypeKind::UNION {
Expand Down Expand Up @@ -6654,7 +6654,7 @@ def di_type(tpe: &typechecking::Type, state: &State) -> &Value {
(@state).ditypes(tpe) = ditpep

if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
}
switch tpe.kind !int {
// TODO compare with char for special casing
Expand Down Expand Up @@ -8958,7 +8958,7 @@ def do_create_type(tpe: &typechecking::Type, svalue: &scope::Value, module: &too
if toolchain::no_stdlib { return NO_VALUE }

if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
}

let generic = typechecking::get_generic(tpe)
Expand Down Expand Up @@ -9181,7 +9181,7 @@ def do_create_type(tpe: &typechecking::Type, svalue: &scope::Value, module: &too
// Map of ToResolve
let types_to_resolve = map::make(ToResolve)
type ToResolve = struct {
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
tpe: &typechecking::Type
}

Expand All @@ -9204,13 +9204,13 @@ export def resolve_types {
type TypeEntry = struct {
tpe: &typechecking::Type
value: &scope::Value
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
}

export def create_type(tpe: &typechecking::Type, module: &toolchain::Module, cache: &Vector(TypeEntry)) -> &Value {
if not tpe { return null }
if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
}
if tpe.tc_incomplete { return null }
let generic = typechecking::get_generic(tpe)
Expand Down
2 changes: 1 addition & 1 deletion src/consteval.pr
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export def walk_Def(node: &parser::Node, state: &typechecking::State) {
is_closure = node.parent.kind != parser::NodeKind::PROGRAM,
scope = state.scope,
inner_scope = node.inner_scope,
captures = vector::make(type weak_ref(scope::Value)),
captures = vector::make(type weak &scope::Value),
dllimport = dllimport,
dllexport = dllexport or test,
test = test,
Expand Down
2 changes: 1 addition & 1 deletion src/debug.pr
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ export def type_to_str(tpe: &typechecking::Type, full_name: bool = false) -> Str
if tpe.tc_tpe { return tc_args_to_string(tpe, full_name) }
switch tpe.kind !int {
case typechecking::TypeKind::BOX
return "Box<" + type_to_str(tpe.weak, full_name) + ">"
return "Box<" + type_to_str(tpe.wk, full_name) + ">"
case typechecking::TypeKind::VOID
return "void"
case typechecking::TypeKind::BOOL
Expand Down
4 changes: 2 additions & 2 deletions src/lexer.pr
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export type TokenType = enum {
K_ASSERT
K_TYPE_OF
K_UNDEF
K_WEAK_REF
K_REF
K_WEAK
INTEGER
FLOAT
STRING
Expand Down Expand Up @@ -224,7 +224,7 @@ let KEYWORDS = [
[token_type = TokenType::K_DEFINED, str = "defined"] !Keyword,
[token_type = TokenType::K_DEFER, str = "defer"] !Keyword,
[token_type = TokenType::K_TYPE_OF, str = "type_of"] !Keyword,
[token_type = TokenType::K_WEAK_REF, str = "weak_ref"] !Keyword,
[token_type = TokenType::K_WEAK, str = "weak"] !Keyword,
[token_type = TokenType::K_YIELD, str = "yield"] !Keyword,
[token_type = TokenType::K_IMPLICIT, str = "implicit"] !Keyword,
[token_type = TokenType::K_REF, str = "ref"] !Keyword
Expand Down
38 changes: 6 additions & 32 deletions src/parser.pr
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ export type Node = struct {
// For things like if statements or functions
inner_scope: &scope::Scope
value: NodeValue
parent: weak_ref(Node)
parent: weak &Node

// Set to prevent destructors being called on uninitalized values
is_initializer: bool
Expand All @@ -475,7 +475,7 @@ export type Node = struct {
body: &Vector(&Node)
// This is used by assignment to track let
kw: VarDecl
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
is_recursive_type: bool

// Need to keep copied type nodes in the tree
Expand Down Expand Up @@ -1856,43 +1856,17 @@ def expect_array_or_tuple(parse_state: &ParseState) -> &Node {
}

def expect_weak_ref(parse_state: &ParseState, inline_types: bool) -> &Node {
var tok = expect(parse_state, lexer::TokenType::K_WEAK_REF, "Expected weak_ref")
var tok = expect(parse_state, lexer::TokenType::K_WEAK, "Expected weak")

let line = tok.line
let column = tok.column

var tpe: &Node = null
var kw = VarDecl::VAR
tok = peek(parse_state)
if tok.tpe == lexer::TokenType::O_PAREN {
pop(parse_state)
skip_newline(parse_state)
tok = peek(parse_state)
if tok.tpe == lexer::TokenType::K_VAR {
pop(parse_state)
skip_newline(parse_state)
} else if tok.tpe == lexer::TokenType::K_LET {
pop(parse_state)
kw = VarDecl::LET
skip_newline(parse_state)
}

var tokens = parse_state.tokens
tpe = parse_type(parse_state, inline_types)
if not tpe {
parse_state.tokens = tokens
}

skip_newline(parse_state)
expect(parse_state, lexer::TokenType::C_PAREN, "Expected ')'")
}
let tpe = expect_type(parse_state)

var node = make_node(NodeKind::WEAK_REF_T, line, column, parse_state)
node.value.t_parr = [
kw = kw,
tpe = tpe
] !NodePtrArrayT
node._hash = combine_hashes(node.kind !uint64, kw !uint64, hash(tpe))
node._hash = combine_hashes(node.kind !uint64, hash(tpe))

return node
}
Expand Down Expand Up @@ -2414,7 +2388,7 @@ def parse_type2(parse_state: &ParseState, inline_types: bool) -> &Node {
tok.tpe == lexer::TokenType::OP_BAND {
back(parse_state)
return expect_ptr_ref(parse_state, tok.tpe == lexer::TokenType::OP_BAND, inline_types)
} else if tok.tpe == lexer::TokenType::K_WEAK_REF {
} else if tok.tpe == lexer::TokenType::K_WEAK {
back(parse_state)
return expect_weak_ref(parse_state, inline_types)
} else if tok.tpe == lexer::TokenType::DOUBLE_COLON or
Expand Down
20 changes: 10 additions & 10 deletions src/scope.pr
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export type Ident = struct {
name: Str
signature: Str
_hash: uint64
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
}

export type ImportedModule = struct {
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
alias: &parser::Node
}

Expand Down Expand Up @@ -72,7 +72,7 @@ export def find(ident: Ident) -> &Value {

export type Value = struct {
// This is needed to store the location
name_node: weak_ref(parser::Node)
name_node: weak &parser::Node
share: parser::ShareMarker
modifier: parser::VarDecl
// Name used by the source code
Expand All @@ -85,7 +85,7 @@ export type Value = struct {
tpe: &typechecking::Type
value: &compiler::Value
_scope: &Scope
_module: weak_ref(Scope) // This is needed for modules referencing each other
_module: weak &Scope // This is needed for modules referencing each other
// In case multiple values share one name (overloaded functions)
next: &Value
phase: Phase
Expand All @@ -97,7 +97,7 @@ export type Value = struct {
dllimport: bool
dllexport: bool
// Definition of the value
node: weak_ref(parser::Node)
node: weak &parser::Node
state: &typechecking::State
// For imported scopes, this checks if a value is exported before accessing it
// Also used by serialize to signal that a function is imported (ffi)
Expand All @@ -122,7 +122,7 @@ export type Value = struct {
references: &Vector(parser::SourceLoc)
// Don't serialize these
is_generated: bool
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
}

export def assembly_name(value: &Value, state: &compiler::State) -> Str {
Expand Down Expand Up @@ -198,16 +198,16 @@ export def find_references_to(value: &Value) -> &Vector(parser::SourceLoc) {
}

export type ReExport = struct {
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
pattern: &parser::Node
}

export type Scope = struct {
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
is_function: bool
// Counter for local scopes (shadowing)
scope_count: int
parent: weak_ref(Scope)
parent: weak &Scope
fields: &SMap(&Value)
implicits: &Vector(&Value)
// This is a list of imported scopes,
Expand Down Expand Up @@ -938,7 +938,7 @@ export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &V
for var i in 0..found_member.return_t.length {
var tpe = found_member.return_t(i)
if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
}
return_t.push(typechecking::copy(tpe))
}
Expand Down
8 changes: 4 additions & 4 deletions src/serialize.pr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import util
import builtins

type Serialize = struct {
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
types: &Map(&typechecking::Type, int64)
dependencies: &Set(scope::Ident)
}
Expand All @@ -25,7 +25,7 @@ def add_type(tpe: &typechecking::Type, state: &Serialize) -> int64 {
if not tpe { return -1 }

if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
assert tpe.kind != typechecking::TypeKind::BOX
}

Expand Down Expand Up @@ -139,7 +139,7 @@ def serialize_type(fp: File, tpe: &typechecking::Type, state: &Serialize) {
return
}
if tpe.kind == typechecking::TypeKind::BOX {
tpe = tpe.weak
tpe = tpe.wk
}
assert tpe.kind != typechecking::TypeKind::BOX

Expand Down Expand Up @@ -1091,7 +1091,7 @@ type TypeEntry = struct {
}

export type Deserialize = struct {
module: weak_ref(toolchain::Module)
module: weak &toolchain::Module
types: &Vector(&typechecking::Type)
nodes: &Vector(&parser::Node) // Strong references to nodes
scopes: &Vector(&scope::Scope) // Strong references to enum scopes
Expand Down
8 changes: 4 additions & 4 deletions src/toolchain.pr
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export type Module = struct {
difile: &compiler::Value
stage: Stage
imports: &Set(Str)
dependants: &Set(weak_ref(Module))
dependants: &Set(weak &Module)
// List of Type
// This is a list of functions that are generated for dynamic dispatch
dyn_dispatch_consteval: &Vector(&typechecking::Type)
Expand All @@ -225,7 +225,7 @@ export type Module = struct {
// This is needed to generate functions from create_destructor
compiler_state: &compiler::State
state: &typechecking::State
unresolved: &Map(scope::Ident, weak_ref(scope::Value))
unresolved: &Map(scope::Ident, weak &scope::Value)
// Incremental compilation
// This is set to true if the source file is newer than the cache or
// any of the dependencies changed
Expand Down Expand Up @@ -304,10 +304,10 @@ export def make_module(
code = compiler::make_block(),
imported = set::make(),
imports = set::make(Str),
dependants = set::make(type weak_ref(Module)),
dependants = set::make(type weak &Module),
dyn_dispatch_consteval = vector::make(type &typechecking::Type),
dyn_dispatch = vector::make(type &typechecking::Type),
unresolved = map::make(scope::Ident, type weak_ref(scope::Value)),
unresolved = map::make(scope::Ident, type weak &scope::Value),
inlay_hints = vector::make(type &parser::Node),
closures = vector::make(type &scope::Value),
is_dirty = no_incremental,
Expand Down
Loading

0 comments on commit 5af28c1

Please sign in to comment.