Skip to content

Commit

Permalink
run v fmt -w . to update the source to the latest version of vfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Nov 25, 2023
1 parent 0327262 commit 90b5f50
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 141 deletions.
8 changes: 3 additions & 5 deletions all_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import os
import term
import v.util.diff

const (
go2v_path = @VMODROOT
go2v_exe = prepare_go2v_executable()
diffcmd = diff.find_working_diff_command()!
)
const go2v_path = @VMODROOT
const go2v_exe = prepare_go2v_executable()
const diffcmd = diff.find_working_diff_command()!

fn prepare_go2v_executable() string {
go2v_source := '${go2v_path}/go2v.v'
Expand Down
4 changes: 1 addition & 3 deletions go2v.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import os
import cli
import transpiler

const (
go2v_path = @VMODROOT
)
const go2v_path = @VMODROOT

fn main() {
mut app := cli.Command{
Expand Down
16 changes: 7 additions & 9 deletions paths_test.v
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os

const (
hello_go = 'package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println("hello")\n}\n'
go2v_path = @VMODROOT
go2v_exe = prepare_go2v_executable()
path_test_area = os.join_path_single(go2v_path, 'path_test_area')
go_test_file = os.join_path_single(path_test_area, 'hello.go')
quoted_go_test_file = os.quoted_path(go_test_file)
quoted_path_test_area = os.quoted_path(path_test_area)
)
const hello_go = 'package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println("hello")\n}\n'
const go2v_path = @VMODROOT
const go2v_exe = prepare_go2v_executable()
const path_test_area = os.join_path_single(go2v_path, 'path_test_area')
const go_test_file = os.join_path_single(path_test_area, 'hello.go')
const quoted_go_test_file = os.quoted_path(go_test_file)
const quoted_path_test_area = os.quoted_path(path_test_area)

fn prepare_go2v_executable() string {
go2v_source := '${go2v_path}/go2v.v'
Expand Down
4 changes: 1 addition & 3 deletions transpiler/ast_extractor.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module transpiler
// `true` will be interpreted as a `bool` by default
// `12` will not be interpreted as a `u32` by default
// any other type than those ones may require casting
const (
well_interpreted_types = ['bool', 'string', 'rune', 'int']
)
const well_interpreted_types = ['bool', 'string', 'rune', 'int']

// entry point for the AST extraction phase
fn ast_extractor(tree Tree) VAST {
Expand Down
194 changes: 96 additions & 98 deletions transpiler/ast_transformer.v
Original file line number Diff line number Diff line change
@@ -1,104 +1,102 @@
module transpiler

const (
// `name` as a key includes `name`
// `name.` as a key includes `name`, `name.foo`, `name.foo.bar`...
// `name` as a value transforms to `name`
// `name.` as a key transforms to `name`, `name.foo`, `name.foo.bar`...
// `` as a key removes the import
// `nameInGo`: `nameInV`
module_equivalence = {
'archive.': 'compress.'
'bufio': 'io'
'bytes': ''
'compress.flate': 'compress.deflate'
'container.': 'datatypes'
'database.': ''
'debug.': ''
'embed': ''
'encoding.json': 'json'
'errors': ''
'expvar': ''
'fmt': ''
'@go.': 'v.'
'html.': ''
'image.': 'gg'
'index.': ''
'io.fs': 'os'
'io.ioutil': 'io.util'
'log.syslog': ''
'math.cmplx': 'math.complex'
'math.rand': 'rand'
'mime.': 'net.http.mime'
'net.http.cgi': 'net.http'
'net.http.cookiejar': 'net.http'
'net.http.fcgi': 'net.http'
'net.http.httptest': ''
'net.http.httptrace': ''
'net.http.httputil': 'net.http'
'net.http.pprof': ''
'net.mail': 'net.smtp'
'net.netip': ''
'net.rpc.': ''
'net.textproto': ''
'net.url': 'net.urllib'
'os.': 'os'
'path.': 'os'
'plugin': ''
'reflect': ''
'regexp.': 'regex'
'runtime.': ''
'sort': ''
'sync.atomic': 'sync.stdatomic'
'syscall.': ''
'testing.': ''
'text.': 'strings'
'time.tzdata': 'time'
'unicode.': 'encoding.'
'unsafe': ''
'internal.': ''
}
// functions that are in the `strings` module in Go and in the `builtin` module in V
strings_to_builtin = ['compare', 'contains', 'contains_any', 'count', 'fields', 'index',
'index_any', 'index_byte', 'last_index', 'last_index_byte', 'repeat', 'split', 'title',
'to_lower', 'to_upper', 'trim', 'trim_left', 'trim_prefix', 'trim_right', 'trim_space',
'trim_suffix']
// function name equivalence (left Go & right V)
name_equivalence = {
'string': 'str'
'rune': 'runes'
}
// methods of the string builder that require a special treatment
string_builder_diffs = ['cap', 'grow', 'len', 'reset', 'string', 'write']
// equivalent of Go's `unicode.utf8.EncodeRune()`
//
// fn go2v_utf8_encode_rune(mut p []u8, r rune) int {
// mut bytes := r.bytes()
// p << bytes
// return bytes.len
// }
go2v_utf8_encode_rune = FunctionStmt{
name: 'go2v_utf8_encode_rune'
args: {
'p': 'mut []u8'
'r': 'rune'
}
ret_vals: ['int']
body: [
VariableStmt{
names: ['bytes']
middle: ':='
values: [CallStmt{
namespaces: 'r.bytes'
}]
},
PushStmt{BasicValueStmt{'p'}, BasicValueStmt{'bytes'}},
ReturnStmt{
values: [BasicValueStmt{'bytes.len'}]
},
]
// `name` as a key includes `name`
// `name.` as a key includes `name`, `name.foo`, `name.foo.bar`...
// `name` as a value transforms to `name`
// `name.` as a key transforms to `name`, `name.foo`, `name.foo.bar`...
// `` as a key removes the import
// `nameInGo`: `nameInV`
const module_equivalence = {
'archive.': 'compress.'
'bufio': 'io'
'bytes': ''
'compress.flate': 'compress.deflate'
'container.': 'datatypes'
'database.': ''
'debug.': ''
'embed': ''
'encoding.json': 'json'
'errors': ''
'expvar': ''
'fmt': ''
'@go.': 'v.'
'html.': ''
'image.': 'gg'
'index.': ''
'io.fs': 'os'
'io.ioutil': 'io.util'
'log.syslog': ''
'math.cmplx': 'math.complex'
'math.rand': 'rand'
'mime.': 'net.http.mime'
'net.http.cgi': 'net.http'
'net.http.cookiejar': 'net.http'
'net.http.fcgi': 'net.http'
'net.http.httptest': ''
'net.http.httptrace': ''
'net.http.httputil': 'net.http'
'net.http.pprof': ''
'net.mail': 'net.smtp'
'net.netip': ''
'net.rpc.': ''
'net.textproto': ''
'net.url': 'net.urllib'
'os.': 'os'
'path.': 'os'
'plugin': ''
'reflect': ''
'regexp.': 'regex'
'runtime.': ''
'sort': ''
'sync.atomic': 'sync.stdatomic'
'syscall.': ''
'testing.': ''
'text.': 'strings'
'time.tzdata': 'time'
'unicode.': 'encoding.'
'unsafe': ''
'internal.': ''
}
// functions that are in the `strings` module in Go and in the `builtin` module in V
const strings_to_builtin = ['compare', 'contains', 'contains_any', 'count', 'fields', 'index',
'index_any', 'index_byte', 'last_index', 'last_index_byte', 'repeat', 'split', 'title',
'to_lower', 'to_upper', 'trim', 'trim_left', 'trim_prefix', 'trim_right', 'trim_space',
'trim_suffix']
// function name equivalence (left Go & right V)
const name_equivalence = {
'string': 'str'
'rune': 'runes'
}
// methods of the string builder that require a special treatment
const string_builder_diffs = ['cap', 'grow', 'len', 'reset', 'string', 'write']
// equivalent of Go's `unicode.utf8.EncodeRune()`
//
// fn go2v_utf8_encode_rune(mut p []u8, r rune) int {
// mut bytes := r.bytes()
// p << bytes
// return bytes.len
// }
const go2v_utf8_encode_rune = FunctionStmt{
name: 'go2v_utf8_encode_rune'
args: {
'p': 'mut []u8'
'r': 'rune'
}
)
ret_vals: ['int']
body: [
VariableStmt{
names: ['bytes']
middle: ':='
values: [CallStmt{
namespaces: 'r.bytes'
}]
},
PushStmt{BasicValueStmt{'p'}, BasicValueStmt{'bytes'}},
ReturnStmt{
values: [BasicValueStmt{'bytes.len'}]
},
]
}

// initialize struct field that are references to themself
fn (mut v VAST) struct_transformer(mut s Struct) {
Expand Down
44 changes: 21 additions & 23 deletions transpiler/utils.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@ module transpiler
import v.token
import strings

const (
// types equivalence (left Go & right V)
get_v_type = {
'bool': 'bool'
'string': 'string'
'byte': 'u8'
'rune': 'rune'
'int': 'int'
'int8': 'i8'
'int16': 'i16'
'int32': 'i32'
'int64': 'i64'
'uint8': 'u8'
'uint16': 'u16'
'uint32': 'u32'
'uint64': 'u64'
'float32': 'f32'
'float64': 'f64'
}
v_types = get_v_type.values()
// keywords to excape with `@`
keywords = token.keywords
)
// types equivalence (left Go & right V)
const get_v_type = {
'bool': 'bool'
'string': 'string'
'byte': 'u8'
'rune': 'rune'
'int': 'int'
'int8': 'i8'
'int16': 'i16'
'int32': 'i32'
'int64': 'i64'
'uint8': 'u8'
'uint16': 'u16'
'uint32': 'u32'
'uint64': 'u64'
'float32': 'f32'
'float64': 'f64'
}
const v_types = get_v_type.values()
// keywords to excape with `@`
const keywords = token.keywords

// used by `set_naming_style()`
enum NamingStyle {
Expand Down

0 comments on commit 90b5f50

Please sign in to comment.