Skip to content

Commit

Permalink
add vector type shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Dec 1, 2024
1 parent 63422c6 commit 6c0c96c
Show file tree
Hide file tree
Showing 8 changed files with 3,115 additions and 2,754 deletions.
5,806 changes: 3,054 additions & 2,752 deletions bootstrap/stage0.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions compiler/lsp/utils.oc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def gen_type_string(type: &Type, full: bool = true): str => match type.base {
else => `&{gen_type_string(type.u.ptr, full)}`
}
Array => `&{gen_type_string(type.u.arr.elem_type, full)}`
VectorShorthand => `$[{gen_type_string(type.u.arr.elem_type, full)}]`
Structure | Alias | Enum => {
if not type.template_instance? return match full {
true => type.sym.display
Expand Down
18 changes: 18 additions & 0 deletions compiler/parser.oc
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ def Parser::parse_type(&this): &Type => match .token().type {
yield typ
}

Dollar => {
let dollar = .consume(Dollar)
yield match .token().type {
OpenSquare => {
.consume(OpenSquare)
let elem_type = .parse_type()
let close = .consume(CloseSquare)
let typ = Type::new_resolved(VectorShorthand, dollar.span.join(close.span))
typ.u.ptr = elem_type
yield typ
}
else => {
.error(Error::new(.token().span, "Unexpected token after `$`"))
yield null
}
}
}

else => {
.unhandled_type("parse_type")
yield Type::new_unresolved_base(BaseType::Error, .token().span)
Expand Down
12 changes: 10 additions & 2 deletions compiler/passes/code_generator.oc
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,18 @@ def CodeGenerator::gen_vector_literal(&this, node: &AST) {

let var = `_vc{.o.program.uid++}`

.out += "({"
.out += "({\n"
.indent += 1
.gen_indent()
.gen_type_and_name(new_method.return_type, var)
.out += " = "
.out += new_method.sym.out_name()
.out <<= `({vec_lit.elements.size});\n`

// FIXME: Make this use default param
let size = vec_lit.elements.size
if size == 0 then size = 8

.out <<= `({size});\n`
for expr in vec_lit.elements.iter() {
.gen_indent()
.out += push_method.sym.out_name()
Expand All @@ -301,6 +308,7 @@ def CodeGenerator::gen_vector_literal(&this, node: &AST) {
.gen_indent()
.out += var
.out += ";})"
.indent -= 1
}

def CodeGenerator::gen_yield_expression(&this, expr: &AST) {
Expand Down
14 changes: 14 additions & 0 deletions compiler/passes/typechecker.oc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ def TypeChecker::resolve_type(

.try_infer_array_size(&resolved.u.arr)
}
VectorShorthand => {
if not (.o.program.did_cache_symbols and .o.program.cached_symbols.std_vector?) {
.error(Error::new(old.span, "Vector shorthand not available"))
resolved = null
} else {
let std_vector = .o.program.cached_symbols.std_vector
let elem_type = .resolve_type(old.u.ptr, p_a, p_e, p_r)

let res = .resolve_templated_symbol(std_vector, $[elem_type], old.span)
assert res.type == Structure
resolved = Type::new_resolved(Pointer, old.span)
resolved.u.ptr = res.u.struc.type
}
}
Structure | Char | Bool | Void | I8 | I16 |
I32 | I64 | U8 | U16 | U32 | U64 | F32 | F64 |
Enum => {}
Expand Down
1 change: 1 addition & 0 deletions compiler/types.oc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum BaseType {
Alias
UnresolvedTemplate
Enum
VectorShorthand

Error
}
Expand Down
6 changes: 6 additions & 0 deletions tests/emoji_var.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// out: "😊 = 10"
def main() {
let πŸ‘€ = 10
println(`😊 = {πŸ‘€}`)
}
11 changes: 11 additions & 0 deletions tests/vector_shorthand.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// out: "3, 4, 0, 2, World"
import std::vector::{ Vector }

def main() {
let x = $[1, 2, 3]
let y: &Vector<i32> = $[1, 2, 3, 4]
let z: &Vector<i32> = $[]
let w: $[str] = $["Hello", "World"]
println(`{x.size}, {y.size}, {z.size}, {w.size}, {w[1]}`)
}

0 comments on commit 6c0c96c

Please sign in to comment.