Skip to content

Commit

Permalink
use sumtype instead of two fields
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 12, 2024
1 parent 1106ca9 commit b5d483f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
17 changes: 9 additions & 8 deletions src/c2v.v
Original file line number Diff line number Diff line change
Expand Up @@ -1758,19 +1758,20 @@ fn (mut c C2V) expr(_node &Node) string {
return ''
}
if node.kindof(.integer_literal) {
if c.returning_bool && node.value in ['1', '0'] {
if node.value == '1' {
value := node.value.to_str()
if c.returning_bool && value in ['1', '0'] {
if value == '1' {
c.gen('true')
} else {
c.gen('false')
}
} else {
c.gen(node.value)
c.gen(value)
}
}
// 'a'
else if node.kindof(.character_literal) {
match rune(node.value_number) {
match rune(node.value as int) {
`\0` { c.gen('`\\0`') }
`\`` { c.gen('`\\``') }
`'` { c.gen("`\\'`") }
Expand All @@ -1783,12 +1784,12 @@ fn (mut c C2V) expr(_node &Node) string {
`\r` { c.gen('`\\r`') }
`\t` { c.gen('`\\t`') }
`\v` { c.gen('`\\v`') }
else { c.gen('`' + rune(node.value_number).str() + '`') }
else { c.gen('`' + rune(node.value as int).str() + '`') }
}
}
// 1e80
else if node.kindof(.floating_literal) {
c.gen(node.value)
c.gen(node.value.to_str())
} else if node.kindof(.constant_expr) {
n := node.try_get_next_child() or {
println(add_place_data_to_error(err))
Expand Down Expand Up @@ -1903,7 +1904,7 @@ fn (mut c C2V) expr(_node &Node) string {
}
// "string literal"
else if node.kindof(.string_literal) {
str := node.value
str := node.value.to_str()
// "a" => 'a'
no_quotes := str.substr(1, str.len - 1)
if no_quotes.contains("'") {
Expand Down Expand Up @@ -2083,7 +2084,7 @@ fn (mut c C2V) expr(_node &Node) string {
print_backtrace()
exit(1)
}
return node.value // get_val(0)
return node.value.to_str() // get_val(0)
}

fn (mut c C2V) name_expr(node &Node) {
Expand Down
4 changes: 2 additions & 2 deletions src/cpp.v
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn (mut c C2V) cpp_expr(_node &Node) bool {
bad_node
}

c.gen(str_lit.value) // get_val(-1))
c.gen(str_lit.value as string) // get_val(-1))
}
} else if node.kindof(.unresolved_lookup_expr) {
} else if node.kindof(.cxx_try_stmt) {
Expand All @@ -165,7 +165,7 @@ fn (mut c C2V) cpp_expr(_node &Node) bool {
c.gen('this')
} else if node.kindof(.cxx_bool_literal_expr) {
val := node.value // get_val(-1)
c.gen(val)
c.gen(val as string)
} else if node.kindof(.cxx_null_ptr_literal_expr) {
c.gen('nullptr')
} else if node.kindof(.cxx_functional_cast_expr) {
Expand Down
13 changes: 11 additions & 2 deletions src/node.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module main

type Value = string | int

// vfmt off
struct Node {
id string
Expand All @@ -13,8 +15,7 @@ struct Node {
class_modifier string @[json: 'storageClass']
tags string @[json: 'tagUsed']
initialization_type string @[json: 'init'] // "c" => "cinit"
value string // e.g. "777" for IntegerLiteral
value_number int @[json: 'value'] // For CharacterLiterals, since `value` is a number there, not at string
value Value @[json: 'value'] // For CharacterLiterals, since `value` is a number there, not at string
opcode string // e.g. "+" in BinaryOperator
ast_argument_type AstJsonType @[json: 'argType']
declaration_id string @[json: 'declId'] // for goto labels
Expand Down Expand Up @@ -93,6 +94,14 @@ const bad_node = Node{
kind: .bad
}

fn (value Value) to_str() string {
if value is int {
return value.str()
} else {
return value as string
}
}

fn (node Node) kindof(expected_kind NodeKind) bool {
return node.kind == expected_kind
}
Expand Down

0 comments on commit b5d483f

Please sign in to comment.