v: clean up enum vals; make array_init return array

pull/3361/head
joe-conigliaro 2020-01-08 01:46:57 +11:00 committed by Alexander Medvednikov
parent fb0817277f
commit 2ab7b40f2f
9 changed files with 102 additions and 94 deletions

View File

@ -9,11 +9,11 @@ import (
strings strings
filepath filepath
//compiler.x64 //compiler.x64
v.gen.x64 // v.gen.x64
//v.types //v.types
v.table // v.table
v.parser // v.parser
v.gen // v.gen
time time
) )
@ -382,6 +382,8 @@ pub fn (v mut V) compile() {
v.cc() v.cc()
} }
pub fn (v &V) compile2() {}
/*
pub fn (v mut V) compile2() { pub fn (v mut V) compile2() {
if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' { if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' {
verror('Cannot build with msvc on ${os.user_os()}') verror('Cannot build with msvc on ${os.user_os()}')
@ -416,7 +418,10 @@ pub fn (v mut V) compile2() {
v.cc() v.cc()
} }
*/
pub fn (v &V) compile_x64() {}
/*
pub fn (v mut V) compile_x64() { pub fn (v mut V) compile_x64() {
$if !linux { $if !linux {
println('v -x64 can only generate Linux binaries for now') println('v -x64 can only generate Linux binaries for now')
@ -440,6 +445,7 @@ pub fn (v mut V) compile_x64() {
} }
*/ */
} }
*/
fn (v mut V) generate_init() { fn (v mut V) generate_init() {
$if js { $if js {

View File

@ -249,7 +249,7 @@ fn (g mut Gen) expr(node ast.Expr) {
// If expression? Assign the value to a temp var. // If expression? Assign the value to a temp var.
// Previously ?: was used, but it's too unreliable. // Previously ?: was used, but it's too unreliable.
mut tmp := '' mut tmp := ''
if it.ti.kind != ._void { if it.ti.kind != .void {
tmp = g.table.new_tmp_var() tmp = g.table.new_tmp_var()
// g.writeln('$it.ti.name $tmp;') // g.writeln('$it.ti.name $tmp;')
} }
@ -258,7 +258,7 @@ fn (g mut Gen) expr(node ast.Expr) {
g.writeln(') {') g.writeln(') {')
for i, stmt in it.stmts { for i, stmt in it.stmts {
// Assign ret value // Assign ret value
if i == it.stmts.len - 1 && it.ti.kind != ._void { if i == it.stmts.len - 1 && it.ti.kind != .void {
// g.writeln('$tmp =') // g.writeln('$tmp =')
println(1) println(1)
} }

View File

@ -27,7 +27,7 @@ void foo(int a) {
i < 10; i++; i < 10; i++;
) { ) {
} }
int nums = new_array_from_c_array(3, 3, sizeof(int), { array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), {
1, 2, 3, 1, 2, 3,
}); });
int number = nums[0]; int number = nums[0];

View File

@ -65,7 +65,7 @@ void function2() {
} }
void init_array() { void init_array() {
int nums = new_array_from_c_array(3, 3, sizeof(int), { array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), {
1, 2, 3, 1, 2, 3,
}); });
} }

View File

@ -12,7 +12,7 @@ pub fn (p mut Parser) parse_array_ti(nr_muls int) types.TypeIdent {
p.check(.rsbr) p.check(.rsbr)
elem_ti := p.parse_ti() elem_ti := p.parse_ti()
idx,name := p.table.find_or_register_array_fixed(&elem_ti, size, 1) idx,name := p.table.find_or_register_array_fixed(&elem_ti, size, 1)
return types.new_ti(._array_fixed, name, idx, nr_muls) return types.new_ti(.array_fixed, name, idx, nr_muls)
} }
// array // array
p.check(.rsbr) p.check(.rsbr)
@ -24,7 +24,7 @@ pub fn (p mut Parser) parse_array_ti(nr_muls int) types.TypeIdent {
nr_dims++ nr_dims++
} }
idx,name := p.table.find_or_register_array(&elem_ti, nr_dims) idx,name := p.table.find_or_register_array(&elem_ti, nr_dims)
return types.new_ti(._array, name, idx, nr_muls) return types.new_ti(.array, name, idx, nr_muls)
} }
pub fn (p mut Parser) parse_map_ti(nr_muls int) types.TypeIdent { pub fn (p mut Parser) parse_map_ti(nr_muls int) types.TypeIdent {
@ -34,7 +34,7 @@ pub fn (p mut Parser) parse_map_ti(nr_muls int) types.TypeIdent {
p.check(.rsbr) p.check(.rsbr)
value_ti := p.parse_ti() value_ti := p.parse_ti()
idx,name := p.table.find_or_register_map(&key_ti, &value_ti) idx,name := p.table.find_or_register_map(&key_ti, &value_ti)
return types.new_ti(._map, name, idx, nr_muls) return types.new_ti(.map, name, idx, nr_muls)
} }
pub fn (p mut Parser) parse_multi_return_ti() types.TypeIdent { pub fn (p mut Parser) parse_multi_return_ti() types.TypeIdent {
@ -52,14 +52,14 @@ pub fn (p mut Parser) parse_multi_return_ti() types.TypeIdent {
} }
p.check(.rpar) p.check(.rpar)
idx,name := p.table.find_or_register_multi_return(mr_tis) idx,name := p.table.find_or_register_multi_return(mr_tis)
return types.new_ti(._multi_return, name, idx, 0) return types.new_ti(.multi_return, name, idx, 0)
} }
pub fn (p mut Parser) parse_variadic_ti() types.TypeIdent { pub fn (p mut Parser) parse_variadic_ti() types.TypeIdent {
p.check(.ellipsis) p.check(.ellipsis)
variadic_ti := p.parse_ti() variadic_ti := p.parse_ti()
idx,name := p.table.find_or_register_variadic(&variadic_ti) idx,name := p.table.find_or_register_variadic(&variadic_ti)
return types.new_ti(._variadic, name, idx, 0) return types.new_ti(.variadic, name, idx, 0)
} }
pub fn (p mut Parser) parse_ti() types.TypeIdent { pub fn (p mut Parser) parse_ti() types.TypeIdent {
@ -98,52 +98,52 @@ pub fn (p mut Parser) parse_ti() types.TypeIdent {
return p.parse_map_ti(nr_muls) return p.parse_map_ti(nr_muls)
} }
'voidptr' { 'voidptr' {
return types.new_builtin_ti(._voidptr, nr_muls) return types.new_builtin_ti(.voidptr, nr_muls)
} }
'byteptr' { 'byteptr' {
return types.new_builtin_ti(._byteptr, nr_muls) return types.new_builtin_ti(.byteptr, nr_muls)
} }
'charptr' { 'charptr' {
return types.new_builtin_ti(._charptr, nr_muls) return types.new_builtin_ti(.charptr, nr_muls)
} }
'i8' { 'i8' {
return types.new_builtin_ti(._i8, nr_muls) return types.new_builtin_ti(.i8, nr_muls)
} }
'i16' { 'i16' {
return types.new_builtin_ti(._i16, nr_muls) return types.new_builtin_ti(.i16, nr_muls)
} }
'int' { 'int' {
return types.new_builtin_ti(._int, nr_muls) return types.new_builtin_ti(.int, nr_muls)
} }
'i64' { 'i64' {
return types.new_builtin_ti(._i64, nr_muls) return types.new_builtin_ti(.i64, nr_muls)
} }
'byte' { 'byte' {
return types.new_builtin_ti(._byte, nr_muls) return types.new_builtin_ti(.byte, nr_muls)
} }
'u16' { 'u16' {
return types.new_builtin_ti(._u16, nr_muls) return types.new_builtin_ti(.u16, nr_muls)
} }
'u32' { 'u32' {
return types.new_builtin_ti(._u32, nr_muls) return types.new_builtin_ti(.u32, nr_muls)
} }
'u64' { 'u64' {
return types.new_builtin_ti(._u64, nr_muls) return types.new_builtin_ti(.u64, nr_muls)
} }
'f32' { 'f32' {
return types.new_builtin_ti(._f32, nr_muls) return types.new_builtin_ti(.f32, nr_muls)
} }
'f64' { 'f64' {
return types.new_builtin_ti(.f64, nr_muls) return types.new_builtin_ti(.f64, nr_muls)
} }
'string' { 'string' {
return types.new_builtin_ti(._string, nr_muls) return types.new_builtin_ti(.string, nr_muls)
} }
'char' { 'char' {
return types.new_builtin_ti(._char, nr_muls) return types.new_builtin_ti(.char, nr_muls)
} }
'bool' { 'bool' {
return types.new_builtin_ti(._bool, nr_muls) return types.new_builtin_ti(.bool, nr_muls)
} }
// struct / enum / placeholder // struct / enum / placeholder
else { else {
@ -153,7 +153,7 @@ pub fn (p mut Parser) parse_ti() types.TypeIdent {
if idx == 0 { if idx == 0 {
idx = p.table.add_placeholder_type(name) idx = p.table.add_placeholder_type(name)
} }
return types.new_ti(._placeholder, name, idx, nr_muls) return types.new_ti(.placeholder, name, idx, nr_muls)
} }
} }
} }

View File

@ -122,7 +122,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
ti: ti ti: ti
name: arg_name name: arg_name
} }
if ti.kind == ._variadic && p.tok.kind == .comma { if ti.kind == .variadic && p.tok.kind == .comma {
p.error('cannot use ...(variadic) with non-final parameter $arg_name') p.error('cannot use ...(variadic) with non-final parameter $arg_name')
} }
} }

View File

@ -435,7 +435,7 @@ fn (p mut Parser) dot_expr(left ast.Expr) (ast.Expr,types.TypeIdent) {
/* /*
// p.next() // p.next()
field := p.check_name() field := p.check_name()
if !ti.type_kind in [._placeholder, ._struct] { if !ti.type_kind in [.placeholder, .struct_] {
println('kind: $ti.str()') println('kind: $ti.str()')
p.error('cannot access field, `$ti.type_name` is not a struct') p.error('cannot access field, `$ti.type_name` is not a struct')
} }
@ -519,7 +519,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
if p.tok.kind != .semicolon { if p.tok.kind != .semicolon {
mut typ := types.TypeIdent{} mut typ := types.TypeIdent{}
cond,typ = p.expr(0) cond,typ = p.expr(0)
if typ.kind != ._bool { if typ.kind != .bool {
p.error('non-bool used as for condition') p.error('non-bool used as for condition')
} }
} }
@ -571,7 +571,7 @@ fn (p mut Parser) if_expr() (ast.Expr,types.TypeIdent) {
p.check(.key_if) p.check(.key_if)
cond,cond_ti := p.expr(0) cond,cond_ti := p.expr(0)
// if !types.check(types.bool_ti, cond_ti) { // if !types.check(types.bool_ti, cond_ti) {
if cond_ti.kind != ._bool { if cond_ti.kind != .bool {
p.error('non-bool used as if condition') p.error('non-bool used as if condition')
} }
stmts := p.parse_block() stmts := p.parse_block()
@ -645,13 +645,15 @@ fn (p mut Parser) array_init() (ast.Expr,types.TypeIdent) {
p.check(.comma) p.check(.comma)
} }
} }
type_idx, type_name := p.table.find_or_register_array(val_ti, 1)
array_ti := types.new_ti(.array, type_name, type_idx, 0)
mut node := ast.Expr{} mut node := ast.Expr{}
node = ast.ArrayInit{ node = ast.ArrayInit{
ti: val_ti ti: array_ti
exprs: exprs exprs: exprs
} }
p.check(.rsbr) p.check(.rsbr)
return node,val_ti return node,array_ti
} }
fn (p mut Parser) parse_number_literal() (ast.Expr,types.TypeIdent) { fn (p mut Parser) parse_number_literal() (ast.Expr,types.TypeIdent) {
@ -752,7 +754,7 @@ fn (p mut Parser) return_stmt() ast.Return {
} }
} }
mut expected_tis := [p.return_ti] mut expected_tis := [p.return_ti]
if p.return_ti.kind == ._multi_return { if p.return_ti.kind == .multi_return {
mr_type := p.table.types[p.return_ti.idx] as types.MultiReturn mr_type := p.table.types[p.return_ti.idx] as types.MultiReturn
expected_tis = mr_type.tis expected_tis = mr_type.tis
} }

View File

@ -78,7 +78,7 @@ pub fn (t mut Table) find_or_register_map(key_ti &types.TypeIdent, value_ti &typ
} }
pub fn (t mut Table) find_or_register_array(elem_ti &types.TypeIdent, nr_dims int) (int,string) { pub fn (t mut Table) find_or_register_array(elem_ti &types.TypeIdent, nr_dims int) (int,string) {
name := 'array_${elem_ti.name}_${nr_dims}d' name := 'array_${elem_ti.name}' + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
// existing // existing
existing_idx := t.type_idxs[name] existing_idx := t.type_idxs[name]
if existing_idx > 0 { if existing_idx > 0 {
@ -100,7 +100,7 @@ pub fn (t mut Table) find_or_register_array(elem_ti &types.TypeIdent, nr_dims in
} }
pub fn (t mut Table) find_or_register_array_fixed(elem_ti &types.TypeIdent, size int, nr_dims int) (int,string) { pub fn (t mut Table) find_or_register_array_fixed(elem_ti &types.TypeIdent, size int, nr_dims int) (int,string) {
name := 'array_fixed_${elem_ti.name}_${size}_${nr_dims}d' name := 'array_fixed_${elem_ti.name}_${size}' + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
// existing // existing
existing_idx := t.type_idxs[name] existing_idx := t.type_idxs[name]
if existing_idx > 0 { if existing_idx > 0 {

View File

@ -4,32 +4,32 @@
module types module types
pub enum Kind { pub enum Kind {
_placeholder placeholder
_void, void,
_voidptr, voidptr,
_charptr, charptr,
_byteptr, byteptr,
_const, const_,
_enum, enum_,
_struct, struct_,
_int, int,
_i8, i8,
_i16, i16,
_i64, i64,
_byte, byte,
_u16, u16,
_u32, u32,
_u64, u64,
_f32, f32,
f64, f64,
_string, string,
_char, char,
_bool, bool,
_array, array,
_array_fixed, array_fixed,
_map, map,
_multi_return, multi_return,
_variadic variadic
} }
pub type Type = Placeholder | Void | Voidptr | Charptr | Byteptr | Const | Enum | Struct | pub type Type = Placeholder | Void | Voidptr | Charptr | Byteptr | Const | Enum | Struct |
@ -56,7 +56,7 @@ pub fn new_ti(kind Kind, name string, idx int, nr_muls int) TypeIdent {
[inline] [inline]
pub fn new_builtin_ti(kind Kind, nr_muls int) TypeIdent { pub fn new_builtin_ti(kind Kind, nr_muls int) TypeIdent {
return TypeIdent{ return TypeIdent{
idx: -int(kind) idx: -int(kind)-1
kind: kind kind: kind
name: kind.str() name: kind.str()
nr_muls: nr_muls nr_muls: nr_muls
@ -70,12 +70,12 @@ pub fn (ti &TypeIdent) is_ptr() bool {
[inline] [inline]
pub fn (ti &TypeIdent) is_int() bool { pub fn (ti &TypeIdent) is_int() bool {
return ti.kind in [._i8, ._i16, ._int, ._i64, ._byte, ._u16, ._u32, ._u64] return ti.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64]
} }
[inline] [inline]
pub fn (ti &TypeIdent) is_float() bool { pub fn (ti &TypeIdent) is_float() bool {
return ti.kind in [._f32, .f64] return ti.kind in [.f32, .f64]
} }
[inline] [inline]
@ -92,7 +92,7 @@ pub fn (ti &TypeIdent) str() string {
} }
pub fn check(got, expected &TypeIdent) bool { pub fn check(got, expected &TypeIdent) bool {
if expected.kind == ._voidptr { if expected.kind == .voidptr {
return true return true
} }
if expected.name == 'array' { if expected.name == 'array' {
@ -106,76 +106,76 @@ pub fn check(got, expected &TypeIdent) bool {
pub fn (k Kind) str() string { pub fn (k Kind) str() string {
k_str := match k { k_str := match k {
._placeholder{ .placeholder{
'placeholder' 'placeholder'
} }
._void{ .void{
'void' 'void'
} }
._voidptr{ .voidptr{
'voidptr' 'voidptr'
} }
._charptr{ .charptr{
'charptr' 'charptr'
} }
._byteptr{ .byteptr{
'byteptr' 'byteptr'
} }
._const{ .const_{
'const' 'const'
} }
._enum{ .enum_{
'enum' 'enum'
} }
._struct{ .struct_{
'struct' 'struct'
} }
._int{ .int{
'int' 'int'
} }
._i8{ .i8{
'i8' 'i8'
} }
._i16{ .i16{
'i16' 'i16'
} }
._i64{ .i64{
'i64' 'i64'
} }
._byte{ .byte{
'byte' 'byte'
} }
._u16{ .u16{
'u18' 'u18'
} }
._f32{ .f32{
'f32' 'f32'
} }
.f64{ .f64{
'f64' 'f64'
} }
._string{ .string{
'string' 'string'
} }
._char{ .char{
'char' 'char'
} }
._bool{ .bool{
'bool' 'bool'
} }
._array{ .array{
'array' 'array'
} }
._array_fixed{ .array_fixed{
'array_fixed' 'array_fixed'
} }
._map{ .map{
'map' 'map'
} }
._multi_return{ .multi_return{
'multi_return' 'multi_return'
} }
._variadic{ .variadic{
'variadic' 'variadic'
} }
else { else {
@ -396,8 +396,8 @@ pub const (
) )
pub const ( pub const (
void_ti = new_builtin_ti(._void, 0) void_ti = new_builtin_ti(.void, 0)
int_ti = new_builtin_ti(._int, 0) int_ti = new_builtin_ti(.int, 0)
string_ti = new_builtin_ti(._string, 0) string_ti = new_builtin_ti(.string, 0)
bool_ti = new_builtin_ti(._bool, 0) bool_ti = new_builtin_ti(.bool, 0)
) )