cgen: optional fixes; make http compile

pull/4312/head^2
Alexander Medvednikov 2020-04-09 14:03:47 +02:00
parent 9e201e1f93
commit d7ae9d7279
2 changed files with 52 additions and 46 deletions

View File

@ -28,11 +28,11 @@ mut:
pub struct FetchConfig { pub struct FetchConfig {
pub mut: pub mut:
method string method string
data string='' data string
params map[string]string=map[string]string params map[string]string
headers map[string]string=map[string]string headers map[string]string
cookies map[string]string=map[string]string cookies map[string]string
user_agent string='v' user_agent string//='v' QTODO
verbose bool=false verbose bool=false
} }

View File

@ -22,39 +22,39 @@ const (
) )
struct Gen { struct Gen {
out strings.Builder out strings.Builder
typedefs strings.Builder typedefs strings.Builder
typedefs2 strings.Builder typedefs2 strings.Builder
definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file) definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file)
inits strings.Builder // contents of `void _vinit(){}` inits strings.Builder // contents of `void _vinit(){}`
gowrappers strings.Builder // all go callsite wrappers gowrappers strings.Builder // all go callsite wrappers
stringliterals strings.Builder // all string literals (they depend on tos3() beeing defined stringliterals strings.Builder // all string literals (they depend on tos3() beeing defined
includes strings.Builder includes strings.Builder
table &table.Table table &table.Table
pref &pref.Preferences pref &pref.Preferences
mut: mut:
file ast.File file ast.File
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
tmp_count int tmp_count int
variadic_args map[string]int variadic_args map[string]int
is_c_call bool // e.g. `C.printf("v")` is_c_call bool // e.g. `C.printf("v")`
is_assign_lhs bool // inside left part of assign expr (for array_set(), etc) is_assign_lhs bool // inside left part of assign expr (for array_set(), etc)
is_assign_rhs bool // inside right part of assign after `=` (val expr) is_assign_rhs bool // inside right part of assign after `=` (val expr)
is_array_set bool is_array_set bool
is_amp bool // for `&Foo{}` to merge PrefixExpr `&` and StructInit `Foo{}`; also for `&byte(0)` etc is_amp bool // for `&Foo{}` to merge PrefixExpr `&` and StructInit `Foo{}`; also for `&byte(0)` etc
optionals []string // to avoid duplicates TODO perf, use map optionals []string // to avoid duplicates TODO perf, use map
inside_ternary bool // ?: comma separated statements on a single line inside_ternary bool // ?: comma separated statements on a single line
stmt_start_pos int stmt_start_pos int
right_is_opt bool right_is_opt bool
autofree bool autofree bool
indent int indent int
empty_line bool empty_line bool
is_test bool is_test bool
assign_op token.Kind // *=, =, etc (for array_set) assign_op token.Kind // *=, =, etc (for array_set)
defer_stmts []ast.DeferStmt defer_stmts []ast.DeferStmt
defer_ifdef string defer_ifdef string
str_types []string // types that need automatic str() generation str_types []string // types that need automatic str() generation
threaded_fns []string // for generating unique wrapper types and fns for `go xxx()` threaded_fns []string // for generating unique wrapper types and fns for `go xxx()`
array_fn_definitions []string // array equality functions that have been defined array_fn_definitions []string // array equality functions that have been defined
} }
@ -1858,7 +1858,8 @@ fn (g mut Gen) return_statement(node ast.Return) {
} }
ast.CallExpr { ast.CallExpr {
// TODO: why? // TODO: why?
if !it.is_method { // if !it.is_method {
if it.name == 'error' {
is_error = true // TODO check name 'error' is_error = true // TODO check name 'error'
} }
} }
@ -2564,9 +2565,15 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
// println(var) or println println(str.var) // println(var) or println println(str.var)
expr := node.args[0].expr expr := node.args[0].expr
is_var := match expr { is_var := match expr {
ast.SelectorExpr { true } ast.SelectorExpr {
ast.Ident { true } true
else { false } }
ast.Ident {
true
}
else {
false
}
} }
// `println(int_str(10))` // `println(int_str(10))`
// sym := g.table.get_type_symbol(node.args[0].typ) // sym := g.table.get_type_symbol(node.args[0].typ)
@ -2597,8 +2604,8 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
g.write('*') g.write('*')
} }
g.expr(expr) g.expr(expr)
if sym.kind ==.struct_ && styp != 'ptr' && !sym.has_method('str') { if sym.kind == .struct_ && styp != 'ptr' && !sym.has_method('str') {
g.write(', 0') // trailing 0 is initial struct indent count g.write(', 0') // trailing 0 is initial struct indent count
} }
} }
g.write('))') g.write('))')
@ -3117,7 +3124,6 @@ fn (g mut Gen) gen_str_for_struct(info table.Struct, styp string) {
g.gen_str_for_type(sym, field_styp) g.gen_str_for_type(sym, field_styp)
} }
} }
s := styp.replace('.', '__') s := styp.replace('.', '__')
g.definitions.write('string ${s}_str($styp it, int indent_count) {\n') g.definitions.write('string ${s}_str($styp it, int indent_count) {\n')
// generate ident / indent length = 4 spaces // generate ident / indent length = 4 spaces
@ -3154,13 +3160,13 @@ fn (g mut Gen) gen_str_for_struct(info table.Struct, styp string) {
fn (g Gen) type_to_fmt(typ table.Type) string { fn (g Gen) type_to_fmt(typ table.Type) string {
sym := g.table.get_type_symbol(typ) sym := g.table.get_type_symbol(typ)
if sym.kind == .struct_ { if sym.kind == .struct_ {
return "%.*s" return '%.*s'
} else if typ == table.string_type { } else if typ == table.string_type {
return "\'%.*s\'" return "\'%.*s\'"
} else if typ == table.bool_type { } else if typ == table.bool_type {
return '%.*s' return '%.*s'
} else if typ in [table.f32_type, table.f64_type] { } else if typ in [table.f32_type, table.f64_type] {
return '%g' // g removes trailing zeros unlike %f return '%g' // g removes trailing zeros unlike %f
} }
return '%d' return '%d'
} }