mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fmt: re-format parser.v and cgen.v
This commit is contained in:
parent
4e447db883
commit
6b2808a3f9
@ -44,7 +44,7 @@ fn main() {
|
||||
foptions := FormatOptions{
|
||||
is_c: '-c' in args
|
||||
is_l: '-l' in args
|
||||
is_w: '-ww' in args
|
||||
is_w: '-w' in args
|
||||
is_diff: '-diff' in args
|
||||
is_verbose: '-verbose' in args || '--verbose' in args
|
||||
is_all: '-all' in args || '--all' in args
|
||||
|
@ -14,33 +14,9 @@ import v.depgraph
|
||||
// NB: keywords after 'new' are reserved in C++
|
||||
const (
|
||||
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
|
||||
'char',
|
||||
'default',
|
||||
'do',
|
||||
'double',
|
||||
'extern',
|
||||
'float',
|
||||
'inline',
|
||||
'int',
|
||||
'long',
|
||||
'register',
|
||||
'restrict',
|
||||
'short',
|
||||
'signed',
|
||||
'sizeof',
|
||||
'static',
|
||||
'switch',
|
||||
'typedef',
|
||||
'union',
|
||||
'unsigned',
|
||||
'void',
|
||||
'volatile',
|
||||
'while',
|
||||
'new',
|
||||
'namespace',
|
||||
'class',
|
||||
'typename'
|
||||
]
|
||||
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
|
||||
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
|
||||
'void', 'volatile', 'while', 'new', 'namespace', 'class', 'typename']
|
||||
// same order as in token.Kind
|
||||
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
|
||||
// when operands are switched
|
||||
@ -436,8 +412,7 @@ typedef struct {
|
||||
.alias {
|
||||
parent := &g.table.types[typ.parent_idx]
|
||||
styp := typ.name.replace('.', '__')
|
||||
is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] ==
|
||||
`.`
|
||||
is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.`
|
||||
parent_styp := if is_c_parent { 'struct ' + parent.name[2..].replace('.', '__') } else { parent.name.replace('.',
|
||||
'__') }
|
||||
g.type_definitions.writeln('typedef $parent_styp $styp;')
|
||||
@ -662,7 +637,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||
}
|
||||
}
|
||||
ast.FnDecl {
|
||||
//g.tmp_count = 0 TODO
|
||||
// g.tmp_count = 0 TODO
|
||||
mut skip := false
|
||||
pos := g.out.buf.len
|
||||
if g.pref.build_mode == .build_module {
|
||||
@ -684,13 +659,15 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||
// just remember `it`; main code will be generated in finish()
|
||||
g.fn_main = node
|
||||
} else {
|
||||
if node.name == 'backtrace' || node.name == 'backtrace_symbols' || node.name ==
|
||||
'backtrace_symbols_fd' {
|
||||
if node.name == 'backtrace' ||
|
||||
node.name == 'backtrace_symbols' ||
|
||||
node.name == 'backtrace_symbols_fd' {
|
||||
g.write('\n#ifndef __cplusplus\n')
|
||||
}
|
||||
g.gen_fn_decl(node)
|
||||
if node.name == 'backtrace' || node.name == 'backtrace_symbols' || node.name ==
|
||||
'backtrace_symbols_fd' {
|
||||
if node.name == 'backtrace' ||
|
||||
node.name == 'backtrace_symbols' ||
|
||||
node.name == 'backtrace_symbols_fd' {
|
||||
g.write('\n#endif\n')
|
||||
}
|
||||
}
|
||||
@ -770,9 +747,9 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||
g.write_autofree_stmts_when_needed(node)
|
||||
g.return_statement(node)
|
||||
}
|
||||
ast.SqlInsertExpr{
|
||||
ast.SqlInsertExpr {
|
||||
g.sql_insert_expr(node)
|
||||
}
|
||||
}
|
||||
ast.StructDecl {
|
||||
name := if node.language == .c { node.name.replace('.', '__') } else { c_name(node.name) }
|
||||
// g.writeln('typedef struct {')
|
||||
@ -915,9 +892,8 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type, expected_type table.Type)
|
||||
got_is_ptr := got_type.is_ptr()
|
||||
expected_is_ptr := expected_type.is_ptr()
|
||||
neither_void := table.voidptr_type !in [got_type, expected_type]
|
||||
if got_is_ptr && !expected_is_ptr && neither_void && expected_sym.kind !in [.interface_,
|
||||
.placeholder
|
||||
] {
|
||||
if got_is_ptr && !expected_is_ptr && neither_void &&
|
||||
expected_sym.kind !in [.interface_, .placeholder] {
|
||||
got_deref_type := got_type.deref()
|
||||
deref_sym := g.table.get_type_symbol(got_deref_type)
|
||||
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
|
||||
@ -1588,9 +1564,6 @@ fn (mut g Gen) expr(node ast.Expr) {
|
||||
ast.SqlExpr {
|
||||
g.sql_select_expr(node)
|
||||
}
|
||||
//ast.SqlInsertExpr {
|
||||
//g.sql_insert_expr(node)
|
||||
//}
|
||||
ast.StringLiteral {
|
||||
if node.is_raw {
|
||||
escaped_val := node.val.replace_each(['"', '\\"', '\\', '\\\\'])
|
||||
@ -1859,8 +1832,8 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
|
||||
}
|
||||
} else if unaliased_left.idx() in [table.u32_type_idx, table.u64_type_idx] && unaliased_right.is_signed() &&
|
||||
node.op in [.eq, .ne, .gt, .lt, .ge, .le] {
|
||||
bitsize := if unaliased_left.idx() == table.u32_type_idx && unaliased_right.idx() !=
|
||||
table.i64_type_idx { 32 } else { 64 }
|
||||
bitsize := if unaliased_left.idx() == table.u32_type_idx &&
|
||||
unaliased_right.idx() != table.i64_type_idx { 32 } else { 64 }
|
||||
g.write('_us${bitsize}_${cmp_str[int(node.op)-int(token.Kind.eq)]}(')
|
||||
g.expr(node.left)
|
||||
g.write(',')
|
||||
@ -1868,8 +1841,8 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
|
||||
g.write(')')
|
||||
} else if unaliased_right.idx() in [table.u32_type_idx, table.u64_type_idx] && unaliased_left.is_signed() &&
|
||||
node.op in [.eq, .ne, .gt, .lt, .ge, .le] {
|
||||
bitsize := if unaliased_right.idx() == table.u32_type_idx && unaliased_left.idx() !=
|
||||
table.i64_type_idx { 32 } else { 64 }
|
||||
bitsize := if unaliased_right.idx() == table.u32_type_idx &&
|
||||
unaliased_left.idx() != table.i64_type_idx { 32 } else { 64 }
|
||||
g.write('_us${bitsize}_${cmp_rev[int(node.op)-int(token.Kind.eq)]}(')
|
||||
g.expr(node.right)
|
||||
g.write(',')
|
||||
@ -1911,8 +1884,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
|
||||
g.writeln('// match 0')
|
||||
return
|
||||
}
|
||||
is_expr := (node.is_expr && node.return_type != table.void_type) || g.inside_ternary >
|
||||
0
|
||||
is_expr := (node.is_expr && node.return_type != table.void_type) || g.inside_ternary > 0
|
||||
if is_expr {
|
||||
g.inside_ternary++
|
||||
// g.write('/* EM ret type=${g.typ(node.return_type)} expected_type=${g.typ(node.expected_type)} */')
|
||||
@ -2395,8 +2367,8 @@ fn (mut g Gen) return_statement(node ast.Return) {
|
||||
// normal return
|
||||
return_sym := g.table.get_type_symbol(node.types[0])
|
||||
// `return opt_ok(expr)` for functions that expect an optional
|
||||
if fn_return_is_optional && !node.types[0].has_flag(.optional) && return_sym.name !=
|
||||
'Option' {
|
||||
if fn_return_is_optional && !node.types[0].has_flag(.optional) &&
|
||||
return_sym.name != 'Option' {
|
||||
styp := g.base_type(g.fn_decl.return_type)
|
||||
opt_type := g.typ(g.fn_decl.return_type)
|
||||
// Create a tmp for this option
|
||||
@ -2994,8 +2966,8 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
|
||||
} else if node.expr_types[i] == table.bool_type {
|
||||
g.expr(expr)
|
||||
g.write(' ? _SLIT("true") : _SLIT("false")')
|
||||
} else if node.expr_types[i].is_number() || node.expr_types[i].is_pointer() || node.fmts[i] ==
|
||||
`d` {
|
||||
} else if node.expr_types[i].is_number() || node.expr_types[i].is_pointer() ||
|
||||
node.fmts[i] == `d` {
|
||||
if node.expr_types[i].is_signed() && node.fmts[i] in [`x`, `X`, `o`] {
|
||||
// convert to unsigned first befors C's integer propagation strikes
|
||||
if node.expr_types[i] == table.i8_type {
|
||||
@ -3306,8 +3278,8 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.
|
||||
g.writeln('\tstring err = ${cvar_name}.v_error;')
|
||||
g.writeln('\tint errcode = ${cvar_name}.ecode;')
|
||||
stmts := or_block.stmts
|
||||
if stmts.len > 0 && stmts[or_block.stmts.len - 1] is ast.ExprStmt && (stmts[stmts.len -
|
||||
1] as ast.ExprStmt).typ != table.void_type {
|
||||
if stmts.len > 0 && stmts[or_block.stmts.len - 1] is ast.ExprStmt &&
|
||||
(stmts[stmts.len - 1] as ast.ExprStmt).typ != table.void_type {
|
||||
g.indent++
|
||||
for i, stmt in stmts {
|
||||
if i == stmts.len - 1 {
|
||||
@ -3510,7 +3482,8 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string {
|
||||
return 'TARGET_ORDER_IS_BIG'
|
||||
}
|
||||
else {
|
||||
if is_comptime_optional || (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) {
|
||||
if is_comptime_optional || (g.pref.compile_defines_all.len > 0 &&
|
||||
name in g.pref.compile_defines_all) {
|
||||
return 'CUSTOM_DEFINE_$name'
|
||||
}
|
||||
verror('bad os ifdef name "$name"')
|
||||
@ -4174,7 +4147,8 @@ fn (mut g Gen) gen_str_for_multi_return(info table.MultiReturn, styp, str_fn_nam
|
||||
sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info()
|
||||
mut arg_str_fn_name := ''
|
||||
if sym_has_str_method {
|
||||
arg_str_fn_name = if is_arg_ptr { field_styp.replace('*', '') + '_str' } else { field_styp + '_str' }
|
||||
arg_str_fn_name = if is_arg_ptr { field_styp.replace('*', '') + '_str' } else { field_styp +
|
||||
'_str' }
|
||||
} else {
|
||||
arg_str_fn_name = styp_to_str_fn_name(field_styp)
|
||||
}
|
||||
@ -4183,7 +4157,7 @@ fn (mut g Gen) gen_str_for_multi_return(info table.MultiReturn, styp, str_fn_nam
|
||||
} else if sym.kind in [.f32, .f64] {
|
||||
g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, _STR("%g", 1, a.arg$i));')
|
||||
} else if sym.kind == .string {
|
||||
g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, a.arg$i));')
|
||||
g.auto_str_funcs.writeln("\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, a.arg$i));")
|
||||
} else {
|
||||
if (str_method_expects_ptr && is_arg_ptr) || (!str_method_expects_ptr && !is_arg_ptr) {
|
||||
g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, ${arg_str_fn_name}(a.arg$i));')
|
||||
|
@ -734,8 +734,9 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt {
|
||||
left0 := left[0]
|
||||
if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() {
|
||||
return p.partial_assign_stmt(left)
|
||||
} else if is_top_level && tok.kind !in [.key_if, .key_match] && left0 !is ast.CallExpr &&
|
||||
left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op == .left_shift) &&
|
||||
} else if is_top_level && tok.kind !in [.key_if, .key_match] &&
|
||||
left0 !is ast.CallExpr && left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr &&
|
||||
(left0 as ast.InfixExpr).op == .left_shift) &&
|
||||
left0 !is ast.ComptimeCall {
|
||||
p.error_with_pos('expression evaluated but not used', left0.position())
|
||||
}
|
||||
@ -848,7 +849,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
|
||||
else {}
|
||||
}
|
||||
}
|
||||
if p.peek_tok.kind == .dot && !known_var && (language != .v || p.known_import(p.tok.lit) ||
|
||||
if p.peek_tok.kind == .dot && !known_var &&
|
||||
(language != .v || p.known_import(p.tok.lit) ||
|
||||
p.mod.all_after_last('.') == p.tok.lit) {
|
||||
if language == .c {
|
||||
mod = 'C'
|
||||
@ -867,7 +869,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
|
||||
}
|
||||
// p.warn('name expr $p.tok.lit $p.peek_tok.str()')
|
||||
// fn call or type cast
|
||||
if p.peek_tok.kind == .lpar || (p.peek_tok.kind == .lt && p.peek_tok2.kind == .name &&
|
||||
if p.peek_tok.kind == .lpar ||
|
||||
(p.peek_tok.kind == .lt && p.peek_tok2.kind == .name &&
|
||||
p.peek_tok3.kind == .gt) {
|
||||
// foo() or foo<int>()
|
||||
mut name := p.tok.lit
|
||||
@ -877,7 +880,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
|
||||
name_w_mod := p.prepend_mod(name)
|
||||
// type cast. TODO: finish
|
||||
// if name in table.builtin_type_names {
|
||||
if !known_var && (name in p.table.type_idxs || name_w_mod in p.table.type_idxs) &&
|
||||
if !known_var && (name in p.table.type_idxs ||
|
||||
name_w_mod in p.table.type_idxs) &&
|
||||
name !in ['C.stat', 'C.sigaction'] {
|
||||
// TODO handle C.stat()
|
||||
mut to_typ := p.parse_type()
|
||||
@ -914,8 +918,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
|
||||
} else if p.peek_tok.kind == .lcbr && !p.inside_match && !p.inside_match_case && !p.inside_if &&
|
||||
!p.inside_for { // && (p.tok.lit[0].is_capital() || p.builtin_mod) {
|
||||
return p.struct_init(false) // short_syntax: false
|
||||
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var && language ==
|
||||
.v) {
|
||||
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var && language == .v) {
|
||||
// `Color.green`
|
||||
mut enum_name := p.check_name()
|
||||
if mod != '' {
|
||||
@ -1389,8 +1392,9 @@ const (
|
||||
// left hand side of `=` or `:=` in `a,b,c := 1,2,3`
|
||||
fn (mut p Parser) global_decl() ast.GlobalDecl {
|
||||
if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v &&
|
||||
p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals &&
|
||||
!p.pref.is_fmt && p.mod !in global_enabled_mods {
|
||||
p.mod != 'ui' && p.mod != 'gg2' &&
|
||||
p.mod != 'uiold' && !p.pref.enable_globals && !p.pref.is_fmt &&
|
||||
p.mod !in global_enabled_mods {
|
||||
p.error('use `v --enable-globals ...` to enable globals')
|
||||
}
|
||||
start_pos := p.tok.position()
|
||||
|
@ -9,6 +9,7 @@ import net
|
||||
import net.http
|
||||
import net.urllib
|
||||
import strings
|
||||
import time
|
||||
|
||||
pub const (
|
||||
methods_with_form = ['POST', 'PUT', 'PATCH']
|
||||
@ -52,6 +53,7 @@ pub mut:
|
||||
pub struct Result {}
|
||||
|
||||
fn (mut ctx Context) send_response_to_client(mimetype string, res string) bool {
|
||||
//println('send_response_to_client(mimetype=$mimetype)')
|
||||
if ctx.done { return false }
|
||||
ctx.done = true
|
||||
mut sb := strings.new_builder(1024)
|
||||
@ -144,7 +146,9 @@ pub fn run_app<T>(mut app T, port int) {
|
||||
for {
|
||||
conn := l.accept() or { panic('accept() failed') }
|
||||
//handle_conn<T>(conn, mut app)
|
||||
//t := time.ticks()
|
||||
handle_conn<T>(conn, mut app)
|
||||
//eprintln('handle conn() took ${time.ticks()-t}ms')
|
||||
// TODO move this to handle_conn<T>(conn, app)
|
||||
//message := readall(conn)
|
||||
//println(message)
|
||||
@ -176,7 +180,7 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
|
||||
//first_line := strip(lines[0])
|
||||
first_line := conn.read_line()
|
||||
$if debug {
|
||||
println('firstline="$first_line"')
|
||||
println('handleconn<T>() firstline="$first_line"')
|
||||
}
|
||||
|
||||
// Parse the first line
|
||||
|
Loading…
Reference in New Issue
Block a user