Skip to content

Commit

Permalink
handle __global x = foo.bar()
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Sep 30, 2024
1 parent 12dbd0d commit 0a03378
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ struct InvalidExpr {}

type Specs = ImportSpec | TypeSpec | ValueSpec

type Type = StructType
type Type = InvalidExpr
| StructType
| ArrayType
| MapType
| FuncType
Expand Down
7 changes: 6 additions & 1 deletion expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn (mut app App) basic_lit(l BasicLit) {

fn quoted_lit(s string, quote string) string {
mut quote2 := quote
go_quote := s[0]
mut no_quotes := s[1..s.len - 1]
// Use "" quotes if the string literal contains '
if quote2 == "'" && no_quotes.contains("'") && !no_quotes.contains('"') {
Expand All @@ -79,8 +80,12 @@ fn quoted_lit(s string, quote string) string {
if s.contains('\\"') {
quote2 = '"'
}
mut prefix := ''
if go_quote == `\`` {
prefix = 'r'
}

return '${quote2}${no_quotes}${quote2}'
return '${prefix}${quote2}${no_quotes}${quote2}'
}

fn (mut app App) selector_expr(s SelectorExpr) {
Expand Down
5 changes: 4 additions & 1 deletion main.v
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ fn (mut app App) typ(t Type) {
app.selector_expr(t)
}
StructType {
app.gen('STRUCT TYPE')
app.gen('STRUCT_TYPE')
}
InvalidExpr {
app.gen('INVALID_EXPR')
}
InterfaceType {
app.interface_type(t)
Expand Down
15 changes: 13 additions & 2 deletions struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn (mut app App) gen_decl(decl GenDecl) {
}
match decl.tok {
'var' {
// app.genln('// ValueSpec global')
app.global_decl(spec)
}
else {
Expand Down Expand Up @@ -63,8 +64,18 @@ fn (mut app App) type_decl(spec TypeSpec) {
fn (mut app App) global_decl(spec ValueSpec) {
for name in spec.names {
app.gen('__global ${name.name} ')
app.typ(spec.typ)
app.genln('')
if spec.typ is InvalidExpr {
// No type means `var x = foo.bar()`
// Eval the expression
app.gen(' = ')
// TODO multiple values?
if spec.values.len > 0 {
app.expr(spec.values[0])
}
} else {
app.typ(spec.typ)
app.genln('')
}
}
}

Expand Down

0 comments on commit 0a03378

Please sign in to comment.