1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v: clean up enum vals; make array_init return array

This commit is contained in:
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
filepath
//compiler.x64
v.gen.x64
// v.gen.x64
//v.types
v.table
v.parser
v.gen
// v.table
// v.parser
// v.gen
time
)
@ -382,6 +382,8 @@ pub fn (v mut V) compile() {
v.cc()
}
pub fn (v &V) compile2() {}
/*
pub fn (v mut V) compile2() {
if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' {
verror('Cannot build with msvc on ${os.user_os()}')
@ -416,7 +418,10 @@ pub fn (v mut V) compile2() {
v.cc()
}
*/
pub fn (v &V) compile_x64() {}
/*
pub fn (v mut V) compile_x64() {
$if !linux {
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() {
$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.
// Previously ?: was used, but it's too unreliable.
mut tmp := ''
if it.ti.kind != ._void {
if it.ti.kind != .void {
tmp = g.table.new_tmp_var()
// g.writeln('$it.ti.name $tmp;')
}
@ -258,7 +258,7 @@ fn (g mut Gen) expr(node ast.Expr) {
g.writeln(') {')
for i, stmt in it.stmts {
// 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 =')
println(1)
}

View File

@ -27,7 +27,7 @@ void foo(int a) {
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,
});
int number = nums[0];

View File

@ -65,7 +65,7 @@ void function2() {
}
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,
});
}

View File

@ -12,7 +12,7 @@ pub fn (p mut Parser) parse_array_ti(nr_muls int) types.TypeIdent {
p.check(.rsbr)
elem_ti := p.parse_ti()
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
p.check(.rsbr)
@ -24,7 +24,7 @@ pub fn (p mut Parser) parse_array_ti(nr_muls int) types.TypeIdent {
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 {
@ -34,7 +34,7 @@ pub fn (p mut Parser) parse_map_ti(nr_muls int) types.TypeIdent {
p.check(.rsbr)
value_ti := p.parse_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 {
@ -52,14 +52,14 @@ pub fn (p mut Parser) parse_multi_return_ti() types.TypeIdent {
}
p.check(.rpar)
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 {
p.check(.ellipsis)
variadic_ti := p.parse_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 {
@ -98,52 +98,52 @@ pub fn (p mut Parser) parse_ti() types.TypeIdent {
return p.parse_map_ti(nr_muls)
}
'voidptr' {
return types.new_builtin_ti(._voidptr, nr_muls)
return types.new_builtin_ti(.voidptr, nr_muls)
}
'byteptr' {
return types.new_builtin_ti(._byteptr, nr_muls)
return types.new_builtin_ti(.byteptr, nr_muls)
}
'charptr' {
return types.new_builtin_ti(._charptr, nr_muls)
return types.new_builtin_ti(.charptr, nr_muls)
}
'i8' {
return types.new_builtin_ti(._i8, nr_muls)
return types.new_builtin_ti(.i8, nr_muls)
}
'i16' {
return types.new_builtin_ti(._i16, nr_muls)
return types.new_builtin_ti(.i16, nr_muls)
}
'int' {
return types.new_builtin_ti(._int, nr_muls)
return types.new_builtin_ti(.int, nr_muls)
}
'i64' {
return types.new_builtin_ti(._i64, nr_muls)
return types.new_builtin_ti(.i64, nr_muls)
}
'byte' {
return types.new_builtin_ti(._byte, nr_muls)
return types.new_builtin_ti(.byte, nr_muls)
}
'u16' {
return types.new_builtin_ti(._u16, nr_muls)
return types.new_builtin_ti(.u16, nr_muls)
}
'u32' {
return types.new_builtin_ti(._u32, nr_muls)
return types.new_builtin_ti(.u32, nr_muls)
}
'u64' {
return types.new_builtin_ti(._u64, nr_muls)
return types.new_builtin_ti(.u64, nr_muls)
}
'f32' {
return types.new_builtin_ti(._f32, nr_muls)
return types.new_builtin_ti(.f32, nr_muls)
}
'f64' {
return types.new_builtin_ti(.f64, nr_muls)
}
'string' {
return types.new_builtin_ti(._string, nr_muls)
return types.new_builtin_ti(.string, nr_muls)
}
'char' {
return types.new_builtin_ti(._char, nr_muls)
return types.new_builtin_ti(.char, nr_muls)
}
'bool' {
return types.new_builtin_ti(._bool, nr_muls)
return types.new_builtin_ti(.bool, nr_muls)
}
// struct / enum / placeholder
else {
@ -153,7 +153,7 @@ pub fn (p mut Parser) parse_ti() types.TypeIdent {
if idx == 0 {
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
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')
}
}

View File

@ -435,7 +435,7 @@ fn (p mut Parser) dot_expr(left ast.Expr) (ast.Expr,types.TypeIdent) {
/*
// p.next()
field := p.check_name()
if !ti.type_kind in [._placeholder, ._struct] {
if !ti.type_kind in [.placeholder, .struct_] {
println('kind: $ti.str()')
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 {
mut typ := types.TypeIdent{}
cond,typ = p.expr(0)
if typ.kind != ._bool {
if typ.kind != .bool {
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)
cond,cond_ti := p.expr(0)
// 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')
}
stmts := p.parse_block()
@ -645,13 +645,15 @@ fn (p mut Parser) array_init() (ast.Expr,types.TypeIdent) {
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{}
node = ast.ArrayInit{
ti: val_ti
ti: array_ti
exprs: exprs
}
p.check(.rsbr)
return node,val_ti
return node,array_ti
}
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]
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
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) {
name := 'array_${elem_ti.name}_${nr_dims}d'
name := 'array_${elem_ti.name}' + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
// existing
existing_idx := t.type_idxs[name]
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) {
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_idx := t.type_idxs[name]
if existing_idx > 0 {

View File

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