run vfmt on cc, cgen, comptime, if_match
parent
ce86626ec2
commit
d38687f893
|
@ -453,7 +453,9 @@ pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
||||||
|
|
||||||
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
||||||
// []int == []int (also for: i64, f32, f64, byte, string)
|
// []int == []int (also for: i64, f32, f64, byte, string)
|
||||||
|
/*
|
||||||
fn array_eq<T>(a1, a2 []T) bool {
|
fn array_eq<T>(a1, a2 []T) bool {
|
||||||
|
|
||||||
if a1.len != a2.len {
|
if a1.len != a2.len {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -484,3 +486,4 @@ pub fn (a []byte) eq(a2 []byte) bool {
|
||||||
pub fn (a []f32) eq(a2 []f32) bool {
|
pub fn (a []f32) eq(a2 []f32) bool {
|
||||||
return array_eq(a, a2)
|
return array_eq(a, a2)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
module compiler
|
module compiler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -11,12 +10,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn todo() {
|
fn todo() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (v &V) no_cc_installed() bool {
|
fn (v &V) no_cc_installed() bool {
|
||||||
$if windows {
|
$if windows {
|
||||||
os.exec('$v.pref.ccompiler -v') or {
|
os.exec('$v.pref.ccompiler -v')or{
|
||||||
if v.pref.is_verbose {
|
if v.pref.is_verbose {
|
||||||
println('C compiler not found, trying to build with msvc...')
|
println('C compiler not found, trying to build with msvc...')
|
||||||
}
|
}
|
||||||
|
@ -49,7 +47,8 @@ fn (v mut V) cc() {
|
||||||
ret := os.system('$vexe -o $vjs_path -os js $vdir/v.v')
|
ret := os.system('$vexe -o $vjs_path -os js $vdir/v.v')
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
println('Done.')
|
println('Done.')
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
println('Failed.')
|
println('Failed.')
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
@ -61,9 +60,10 @@ fn (v mut V) cc() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// v.out_name_c may be on a different partition than v.out_name
|
// v.out_name_c may be on a different partition than v.out_name
|
||||||
os.mv_by_cp(v.out_name_c, v.out_name) or { panic(err) }
|
os.mv_by_cp(v.out_name_c, v.out_name)or{
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
// Cross compiling for Windows
|
// Cross compiling for Windows
|
||||||
|
@ -80,54 +80,47 @@ fn (v mut V) cc() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// arguments for the C compiler
|
// arguments for the C compiler
|
||||||
mut a := [v.pref.cflags, '-std=gnu11',
|
mut a := [v.pref.cflags, '-std=gnu11', '-Wall', '-Wextra',
|
||||||
'-Wall',
|
// TODO : activate -Werror once no warnings remain
|
||||||
'-Wextra',
|
// '-Werror',
|
||||||
// TODO : activate -Werror once no warnings remain
|
// TODO : try and remove the below workaround options when the corresponding
|
||||||
// '-Werror',
|
// warnings are totally fixed/removed
|
||||||
// TODO : try and remove the below workaround options when the corresponding
|
'-Wno-unused-variable',
|
||||||
// warnings are totally fixed/removed
|
// '-Wno-unused-but-set-variable',
|
||||||
'-Wno-unused-variable',
|
'-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces', '-Wno-unused-label']
|
||||||
//'-Wno-unused-but-set-variable',
|
|
||||||
'-Wno-unused-parameter',
|
|
||||||
'-Wno-unused-result',
|
|
||||||
'-Wno-unused-function',
|
|
||||||
'-Wno-missing-braces',
|
|
||||||
'-Wno-unused-label']
|
|
||||||
// TCC on Linux by default, unless -cc was provided
|
// TCC on Linux by default, unless -cc was provided
|
||||||
// TODO if -cc = cc, TCC is still used, default compiler should be
|
// TODO if -cc = cc, TCC is still used, default compiler should be
|
||||||
// used instead.
|
// used instead.
|
||||||
if v.pref.fast {
|
if v.pref.fast {
|
||||||
$if linux {
|
$if linux {
|
||||||
$if !android {
|
$if !android {
|
||||||
tcc_3rd := '$vdir/thirdparty/tcc/bin/tcc'
|
tcc_3rd := '$vdir/thirdparty/tcc/bin/tcc'
|
||||||
//println('tcc third "$tcc_3rd"')
|
// println('tcc third "$tcc_3rd"')
|
||||||
tcc_path := '/var/tmp/tcc/bin/tcc'
|
tcc_path := '/var/tmp/tcc/bin/tcc'
|
||||||
if os.exists(tcc_3rd) && !os.exists(tcc_path) {
|
if os.exists(tcc_3rd) && !os.exists(tcc_path) {
|
||||||
//println('moving tcc')
|
// println('moving tcc')
|
||||||
// if there's tcc in thirdparty/, that means this is
|
// if there's tcc in thirdparty/, that means this is
|
||||||
// a prebuilt V_linux.zip.
|
// a prebuilt V_linux.zip.
|
||||||
// Until the libtcc1.a bug is fixed, we neeed to move
|
// Until the libtcc1.a bug is fixed, we neeed to move
|
||||||
// it to /var/tmp/
|
// it to /var/tmp/
|
||||||
os.system('mv $vdir/thirdparty/tcc /var/tmp/')
|
os.system('mv $vdir/thirdparty/tcc /var/tmp/')
|
||||||
|
}
|
||||||
|
if v.pref.ccompiler == 'cc' && os.exists(tcc_path) {
|
||||||
|
// TODO tcc bug, needs an empty libtcc1.a fila
|
||||||
|
// os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) }
|
||||||
|
// os.create('/var/tmp/tcc/lib/tcc/libtcc1.a')
|
||||||
|
v.pref.ccompiler = tcc_path
|
||||||
|
a << '-m64'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if v.pref.ccompiler == 'cc' && os.exists(tcc_path) {
|
|
||||||
// TODO tcc bug, needs an empty libtcc1.a fila
|
|
||||||
//os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) }
|
|
||||||
//os.create('/var/tmp/tcc/lib/tcc/libtcc1.a')
|
|
||||||
v.pref.ccompiler = tcc_path
|
|
||||||
a << '-m64'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} $else {
|
} $else {
|
||||||
verror('-fast is only supported on Linux right now')
|
verror('-fast is only supported on Linux right now')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//linux_host := os.user_os() == 'linux'
|
// linux_host := os.user_os() == 'linux'
|
||||||
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
|
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
|
||||||
|
|
||||||
if v.pref.is_so {
|
if v.pref.is_so {
|
||||||
a << '-shared -fPIC '// -Wl,-z,defs'
|
a << '-shared -fPIC ' // -Wl,-z,defs'
|
||||||
v.out_name = v.out_name + '.so'
|
v.out_name = v.out_name + '.so'
|
||||||
}
|
}
|
||||||
if v.pref.is_bare {
|
if v.pref.is_bare {
|
||||||
|
@ -135,39 +128,31 @@ fn (v mut V) cc() {
|
||||||
}
|
}
|
||||||
if v.pref.build_mode == .build_module {
|
if v.pref.build_mode == .build_module {
|
||||||
// Create the modules & out directory if it's not there.
|
// Create the modules & out directory if it's not there.
|
||||||
mut out_dir := if v.dir.starts_with('vlib') {
|
mut out_dir := if v.dir.starts_with('vlib') { '$v_modules_path${os.path_separator}cache${os.path_separator}$v.dir' } else { '$v_modules_path${os.path_separator}$v.dir' }
|
||||||
'$v_modules_path${os.path_separator}cache${os.path_separator}$v.dir'
|
|
||||||
} else {
|
|
||||||
'$v_modules_path${os.path_separator}$v.dir'
|
|
||||||
}
|
|
||||||
pdir := out_dir.all_before_last(os.path_separator)
|
pdir := out_dir.all_before_last(os.path_separator)
|
||||||
if !os.is_dir(pdir) {
|
if !os.is_dir(pdir) {
|
||||||
os.mkdir_all(pdir)
|
os.mkdir_all(pdir)
|
||||||
}
|
}
|
||||||
v.out_name = '${out_dir}.o' //v.out_name
|
v.out_name = '${out_dir}.o' // v.out_name
|
||||||
println('Building ${v.out_name}...')
|
println('Building ${v.out_name}...')
|
||||||
}
|
}
|
||||||
|
|
||||||
debug_mode := v.pref.is_debug
|
debug_mode := v.pref.is_debug
|
||||||
mut debug_options := '-g'
|
mut debug_options := '-g'
|
||||||
mut optimization_options := '-O2'
|
mut optimization_options := '-O2'
|
||||||
|
|
||||||
mut guessed_compiler := v.pref.ccompiler
|
mut guessed_compiler := v.pref.ccompiler
|
||||||
if guessed_compiler == 'cc' && v.pref.is_prod {
|
if guessed_compiler == 'cc' && v.pref.is_prod {
|
||||||
// deliberately guessing only for -prod builds for performance reasons
|
// deliberately guessing only for -prod builds for performance reasons
|
||||||
if ccversion := os.exec('cc --version') {
|
if ccversion:=os.exec('cc --version'){
|
||||||
if ccversion.exit_code == 0 {
|
if ccversion.exit_code == 0 {
|
||||||
if ccversion.output.contains('This is free software;')
|
if ccversion.output.contains('This is free software;') && ccversion.output.contains('Free Software Foundation, Inc.') {
|
||||||
&& ccversion.output.contains('Free Software Foundation, Inc.') {
|
|
||||||
guessed_compiler = 'gcc'
|
guessed_compiler = 'gcc'
|
||||||
}
|
}
|
||||||
if ccversion.output.contains('clang version '){
|
if ccversion.output.contains('clang version ') {
|
||||||
guessed_compiler = 'clang'
|
guessed_compiler = 'clang'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.pref.ccompiler.contains('clang') || guessed_compiler == 'clang' {
|
if v.pref.ccompiler.contains('clang') || guessed_compiler == 'clang' {
|
||||||
if debug_mode {
|
if debug_mode {
|
||||||
debug_options = '-g -O0 -no-pie'
|
debug_options = '-g -O0 -no-pie'
|
||||||
|
@ -180,57 +165,60 @@ fn (v mut V) cc() {
|
||||||
}
|
}
|
||||||
optimization_options = '-O3 -fno-strict-aliasing -flto'
|
optimization_options = '-O3 -fno-strict-aliasing -flto'
|
||||||
}
|
}
|
||||||
|
|
||||||
if debug_mode {
|
if debug_mode {
|
||||||
a << debug_options
|
a << debug_options
|
||||||
}
|
}
|
||||||
if v.pref.is_prod {
|
if v.pref.is_prod {
|
||||||
a << optimization_options
|
a << optimization_options
|
||||||
}
|
}
|
||||||
|
if debug_mode && os.user_os() != 'windows' {
|
||||||
if debug_mode && os.user_os() != 'windows'{
|
|
||||||
a << ' -rdynamic ' // needed for nicer symbolic backtraces
|
a << ' -rdynamic ' // needed for nicer symbolic backtraces
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.pref.ccompiler != 'msvc' && v.os != .freebsd {
|
if v.pref.ccompiler != 'msvc' && v.os != .freebsd {
|
||||||
a << '-Werror=implicit-function-declaration'
|
a << '-Werror=implicit-function-declaration'
|
||||||
}
|
}
|
||||||
|
|
||||||
for f in v.generate_hotcode_reloading_compiler_flags() {
|
for f in v.generate_hotcode_reloading_compiler_flags() {
|
||||||
a << f
|
a << f
|
||||||
}
|
}
|
||||||
|
mut libs := '' // builtin.o os.o http.o etc
|
||||||
mut libs := ''// builtin.o os.o http.o etc
|
|
||||||
if v.pref.build_mode == .build_module {
|
if v.pref.build_mode == .build_module {
|
||||||
a << '-c'
|
a << '-c'
|
||||||
}
|
}
|
||||||
else if v.pref.is_cache {
|
else if v.pref.is_cache {
|
||||||
builtin_o_path := filepath.join(v_modules_path, 'cache', 'vlib', 'builtin.o')
|
builtin_o_path := filepath.join(v_modules_path,'cache','vlib','builtin.o')
|
||||||
a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed
|
a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed
|
||||||
if os.exists(builtin_o_path) {
|
if os.exists(builtin_o_path) {
|
||||||
libs = builtin_o_path
|
libs = builtin_o_path
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
println('$builtin_o_path not found... building module builtin')
|
println('$builtin_o_path not found... building module builtin')
|
||||||
os.system('$vexe build module vlib${os.path_separator}builtin')
|
os.system('$vexe build module vlib${os.path_separator}builtin')
|
||||||
}
|
}
|
||||||
for imp in v.table.imports {
|
for imp in v.table.imports {
|
||||||
if imp.contains('vweb') { continue } // not working
|
if imp.contains('vweb') {
|
||||||
if imp == 'webview' { continue }
|
continue
|
||||||
|
} // not working
|
||||||
|
if imp == 'webview' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
imp_path := imp.replace('.', os.path_separator)
|
imp_path := imp.replace('.', os.path_separator)
|
||||||
path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}${imp_path}.o'
|
path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}${imp_path}.o'
|
||||||
//println('adding ${imp_path}.o')
|
// println('adding ${imp_path}.o')
|
||||||
if os.exists(path) {
|
if os.exists(path) {
|
||||||
libs += ' ' + path
|
libs += ' ' + path
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
println('$path not found... building module $imp')
|
println('$path not found... building module $imp')
|
||||||
if path.ends_with('vlib/ui.o') {
|
if path.ends_with('vlib/ui.o') {
|
||||||
println('copying ui...')
|
println('copying ui...')
|
||||||
os.cp('$vdir/thirdparty/ui/ui.o', path) or { panic('error copying ui files') }
|
os.cp('$vdir/thirdparty/ui/ui.o', path)or{
|
||||||
os.cp('$vdir/thirdparty/ui/ui.vh', v_modules_path +
|
panic('error copying ui files')
|
||||||
'/vlib/ui.vh') or { panic('error copying ui files') }
|
}
|
||||||
|
os.cp('$vdir/thirdparty/ui/ui.vh', v_modules_path + '/vlib/ui.vh')or{
|
||||||
} else {
|
panic('error copying ui files')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
os.system('$vexe build module vlib${os.path_separator}$imp_path')
|
os.system('$vexe build module vlib${os.path_separator}$imp_path')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -259,7 +247,7 @@ fn (v mut V) cc() {
|
||||||
// Output executable name
|
// Output executable name
|
||||||
a << '-o "$v.out_name"'
|
a << '-o "$v.out_name"'
|
||||||
if os.is_dir(v.out_name) {
|
if os.is_dir(v.out_name) {
|
||||||
verror('\'$v.out_name\' is a directory')
|
verror("\'$v.out_name\' is a directory")
|
||||||
}
|
}
|
||||||
// macOS code can include objective C TODO remove once objective C is replaced with C
|
// macOS code can include objective C TODO remove once objective C is replaced with C
|
||||||
if v.os == .mac {
|
if v.os == .mac {
|
||||||
|
@ -275,33 +263,27 @@ fn (v mut V) cc() {
|
||||||
a << '-mmacosx-version-min=10.7'
|
a << '-mmacosx-version-min=10.7'
|
||||||
}
|
}
|
||||||
cflags := v.get_os_cflags()
|
cflags := v.get_os_cflags()
|
||||||
|
|
||||||
// add .o files
|
// add .o files
|
||||||
a << cflags.c_options_only_object_files()
|
a << cflags.c_options_only_object_files()
|
||||||
|
|
||||||
// add all flags (-I -l -L etc) not .o files
|
// add all flags (-I -l -L etc) not .o files
|
||||||
a << cflags.c_options_without_object_files()
|
a << cflags.c_options_without_object_files()
|
||||||
|
|
||||||
a << libs
|
a << libs
|
||||||
// Without these libs compilation will fail on Linux
|
// Without these libs compilation will fail on Linux
|
||||||
// || os.user_os() == 'linux'
|
// || os.user_os() == 'linux'
|
||||||
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.os in [ .linux, .freebsd,
|
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] {
|
||||||
.openbsd, .netbsd, .dragonfly, .solaris, .haiku ] {
|
|
||||||
a << '-lm -lpthread '
|
a << '-lm -lpthread '
|
||||||
// -ldl is a Linux only thing. BSDs have it in libc.
|
// -ldl is a Linux only thing. BSDs have it in libc.
|
||||||
if v.os == .linux {
|
if v.os == .linux {
|
||||||
a << ' -ldl '
|
a << ' -ldl '
|
||||||
}
|
}
|
||||||
if v.os == .freebsd {
|
if v.os == .freebsd {
|
||||||
// FreeBSD: backtrace needs execinfo library while linking
|
// FreeBSD: backtrace needs execinfo library while linking
|
||||||
a << ' -lexecinfo '
|
a << ' -lexecinfo '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !v.pref.is_bare && v.os == .js && os.user_os() == 'linux' {
|
if !v.pref.is_bare && v.os == .js && os.user_os() == 'linux' {
|
||||||
a << '-lm'
|
a << '-lm'
|
||||||
}
|
}
|
||||||
|
|
||||||
args := a.join(' ')
|
args := a.join(' ')
|
||||||
start:
|
start:
|
||||||
todo()
|
todo()
|
||||||
|
@ -313,7 +295,7 @@ start:
|
||||||
println(cmd)
|
println(cmd)
|
||||||
}
|
}
|
||||||
ticks := time.ticks()
|
ticks := time.ticks()
|
||||||
res := os.exec(cmd) or {
|
res := os.exec(cmd)or{
|
||||||
// C compilation failed.
|
// C compilation failed.
|
||||||
// If we are on Windows, try msvc
|
// If we are on Windows, try msvc
|
||||||
println('C compilation failed.')
|
println('C compilation failed.')
|
||||||
|
@ -324,6 +306,7 @@ start:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
verror(err)
|
verror(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -337,29 +320,22 @@ start:
|
||||||
goto start
|
goto start
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
verror('C compiler error, while attempting to run: \n' + '-----------------------------------------------------------\n' + '$cmd\n' + '-----------------------------------------------------------\n' + 'Probably your C compiler is missing. \n' + 'Please reinstall it, or make it available in your PATH.\n\n' + missing_compiler_info())
|
||||||
verror('C compiler error, while attempting to run: \n' +
|
|
||||||
'-----------------------------------------------------------\n' +
|
|
||||||
'$cmd\n' +
|
|
||||||
'-----------------------------------------------------------\n' +
|
|
||||||
'Probably your C compiler is missing. \n' +
|
|
||||||
'Please reinstall it, or make it available in your PATH.\n\n' +
|
|
||||||
missing_compiler_info())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.pref.is_debug {
|
if v.pref.is_debug {
|
||||||
println(res.output)
|
println(res.output)
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
partial_output := res.output.limit(200).trim_right('\r\n')
|
partial_output := res.output.limit(200).trim_right('\r\n')
|
||||||
print(partial_output)
|
print(partial_output)
|
||||||
if res.output.len > partial_output.len {
|
if res.output.len > partial_output.len {
|
||||||
println('...\n(Use `v -g` to print the entire error message)\n')
|
println('...\n(Use `v -g` to print the entire error message)\n')
|
||||||
}else{
|
}
|
||||||
|
else {
|
||||||
println('')
|
println('')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
verror('C error. This should never happen. ' +
|
verror('C error. This should never happen. ' + '\nPlease create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
|
||||||
'\nPlease create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
|
|
||||||
}
|
}
|
||||||
diff := time.ticks() - ticks
|
diff := time.ticks() - ticks
|
||||||
// Print the C command
|
// Print the C command
|
||||||
|
@ -389,6 +365,7 @@ start:
|
||||||
println('linux cross compilation done. resulting binary: "$v.out_name"')
|
println('linux cross compilation done. resulting binary: "$v.out_name"')
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if !v.pref.is_keep_c && v.out_name_c != 'v.c' {
|
if !v.pref.is_keep_c && v.out_name_c != 'v.c' {
|
||||||
os.rm(v.out_name_c)
|
os.rm(v.out_name_c)
|
||||||
}
|
}
|
||||||
|
@ -409,8 +386,7 @@ start:
|
||||||
println('install upx with `brew install upx`')
|
println('install upx with `brew install upx`')
|
||||||
}
|
}
|
||||||
$if linux {
|
$if linux {
|
||||||
println('install upx\n' +
|
println('install upx\n' + 'for example, on Debian/Ubuntu run `sudo apt install upx`')
|
||||||
'for example, on Debian/Ubuntu run `sudo apt install upx`')
|
|
||||||
}
|
}
|
||||||
$if windows {
|
$if windows {
|
||||||
// :)
|
// :)
|
||||||
|
@ -419,7 +395,6 @@ start:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn (c mut V) cc_windows_cross() {
|
fn (c mut V) cc_windows_cross() {
|
||||||
if !c.out_name.ends_with('.exe') {
|
if !c.out_name.ends_with('.exe') {
|
||||||
c.out_name = c.out_name + '.exe'
|
c.out_name = c.out_name + '.exe'
|
||||||
|
@ -427,28 +402,20 @@ fn (c mut V) cc_windows_cross() {
|
||||||
mut args := '-o $c.out_name -w -L. '
|
mut args := '-o $c.out_name -w -L. '
|
||||||
cflags := c.get_os_cflags()
|
cflags := c.get_os_cflags()
|
||||||
// -I flags
|
// -I flags
|
||||||
args += if c.pref.ccompiler == 'msvc' {
|
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_before_target_msvc() } else { cflags.c_options_before_target() }
|
||||||
cflags.c_options_before_target_msvc()
|
|
||||||
} else {
|
|
||||||
cflags.c_options_before_target()
|
|
||||||
}
|
|
||||||
mut libs := ''
|
mut libs := ''
|
||||||
if c.pref.build_mode == .default_mode {
|
if c.pref.build_mode == .default_mode {
|
||||||
libs = '"$v_modules_path/vlib/builtin.o"'
|
libs = '"$v_modules_path/vlib/builtin.o"'
|
||||||
if !os.exists(libs) {
|
if !os.exists(libs) {
|
||||||
println('`$libs` not found')
|
println('`$libs` not found')
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
for imp in c.table.imports {
|
for imp in c.table.imports {
|
||||||
libs += ' "$v_modules_path/vlib/${imp}.o"'
|
libs += ' "$v_modules_path/vlib/${imp}.o"'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
args += ' $c.out_name_c '
|
args += ' $c.out_name_c '
|
||||||
args += if c.pref.ccompiler == 'msvc' {
|
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() }
|
||||||
cflags.c_options_after_target_msvc()
|
|
||||||
} else {
|
|
||||||
cflags.c_options_after_target()
|
|
||||||
}
|
|
||||||
println('Cross compiling for Windows...')
|
println('Cross compiling for Windows...')
|
||||||
winroot := '$v_modules_path/winroot'
|
winroot := '$v_modules_path/winroot'
|
||||||
if !os.is_dir(winroot) {
|
if !os.is_dir(winroot) {
|
||||||
|
@ -456,8 +423,7 @@ fn (c mut V) cc_windows_cross() {
|
||||||
println('"$winroot" not found.')
|
println('"$winroot" not found.')
|
||||||
println('Download it from $winroot_url and save it in $v_modules_path')
|
println('Download it from $winroot_url and save it in $v_modules_path')
|
||||||
println('Unzip it afterwards.\n')
|
println('Unzip it afterwards.\n')
|
||||||
println('winroot.zip contains all library and header files needed '+
|
println('winroot.zip contains all library and header files needed ' + 'to cross-compile for Windows.')
|
||||||
'to cross-compile for Windows.')
|
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
mut obj_name := c.out_name
|
mut obj_name := c.out_name
|
||||||
|
@ -466,21 +432,18 @@ fn (c mut V) cc_windows_cross() {
|
||||||
include := '-I $winroot/include '
|
include := '-I $winroot/include '
|
||||||
cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 $v_modules_path/$c.out_name_c'
|
cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 $v_modules_path/$c.out_name_c'
|
||||||
if c.pref.show_c_cmd {
|
if c.pref.show_c_cmd {
|
||||||
println(cmd)
|
println(cmd)
|
||||||
}
|
}
|
||||||
if os.system(cmd) != 0 {
|
if os.system(cmd) != 0 {
|
||||||
println('Cross compilation for Windows failed. Make sure you have clang installed.')
|
println('Cross compilation for Windows failed. Make sure you have clang installed.')
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
if c.pref.build_mode != .build_module {
|
if c.pref.build_mode != .build_module {
|
||||||
link_cmd := 'lld-link $obj_name $winroot/lib/libcmt.lib ' +
|
link_cmd := 'lld-link $obj_name $winroot/lib/libcmt.lib ' + '$winroot/lib/libucrt.lib $winroot/lib/kernel32.lib $winroot/lib/libvcruntime.lib ' + '$winroot/lib/uuid.lib'
|
||||||
'$winroot/lib/libucrt.lib $winroot/lib/kernel32.lib $winroot/lib/libvcruntime.lib ' +
|
|
||||||
'$winroot/lib/uuid.lib'
|
|
||||||
if c.pref.show_c_cmd {
|
if c.pref.show_c_cmd {
|
||||||
println(link_cmd)
|
println(link_cmd)
|
||||||
}
|
}
|
||||||
|
if os.system(link_cmd) != 0 {
|
||||||
if os.system(link_cmd) != 0 {
|
|
||||||
println('Cross compilation for Windows failed. Make sure you have lld linker installed.')
|
println('Cross compilation for Windows failed. Make sure you have lld linker installed.')
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
@ -492,7 +455,7 @@ fn (c mut V) cc_windows_cross() {
|
||||||
fn (c &V) build_thirdparty_obj_files() {
|
fn (c &V) build_thirdparty_obj_files() {
|
||||||
for flag in c.get_os_cflags() {
|
for flag in c.get_os_cflags() {
|
||||||
if flag.value.ends_with('.o') {
|
if flag.value.ends_with('.o') {
|
||||||
rest_of_module_flags := c.get_rest_of_module_cflags( flag )
|
rest_of_module_flags := c.get_rest_of_module_cflags(flag)
|
||||||
if c.pref.ccompiler == 'msvc' || c.no_cc_installed() {
|
if c.pref.ccompiler == 'msvc' || c.no_cc_installed() {
|
||||||
build_thirdparty_obj_file_with_msvc(flag.value, rest_of_module_flags)
|
build_thirdparty_obj_file_with_msvc(flag.value, rest_of_module_flags)
|
||||||
}
|
}
|
||||||
|
@ -506,22 +469,24 @@ fn (c &V) build_thirdparty_obj_files() {
|
||||||
fn find_c_compiler() string {
|
fn find_c_compiler() string {
|
||||||
args := env_vflags_and_os_args().join(' ')
|
args := env_vflags_and_os_args().join(' ')
|
||||||
defaultcc := find_c_compiler_default()
|
defaultcc := find_c_compiler_default()
|
||||||
return get_arg( args, 'cc', defaultcc )
|
return get_arg(args, 'cc', defaultcc)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_c_compiler_default() string {
|
fn find_c_compiler_default() string {
|
||||||
//fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
|
// fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
|
||||||
//if os.exists(fast_clang) {
|
// if os.exists(fast_clang) {
|
||||||
// return fast_clang
|
// return fast_clang
|
||||||
//}
|
// }
|
||||||
// TODO fix $if after 'string'
|
// TODO fix $if after 'string'
|
||||||
$if windows { return 'gcc' }
|
$if windows {
|
||||||
|
return 'gcc'
|
||||||
|
}
|
||||||
return 'cc'
|
return 'cc'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_c_compiler_thirdparty_options() string {
|
fn find_c_compiler_thirdparty_options() string {
|
||||||
fullargs := env_vflags_and_os_args()
|
fullargs := env_vflags_and_os_args()
|
||||||
mut cflags := get_cmdline_cflags( fullargs )
|
mut cflags := get_cmdline_cflags(fullargs)
|
||||||
$if !windows {
|
$if !windows {
|
||||||
cflags += ' -fPIC'
|
cflags += ' -fPIC'
|
||||||
}
|
}
|
||||||
|
@ -535,7 +500,7 @@ fn get_cmdline_cflags(args []string) string {
|
||||||
mut cflags := ''
|
mut cflags := ''
|
||||||
for ci, cv in args {
|
for ci, cv in args {
|
||||||
if cv == '-cflags' {
|
if cv == '-cflags' {
|
||||||
cflags += args[ci+1] + ' '
|
cflags += args[ci + 1] + ' '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cflags
|
return cflags
|
||||||
|
@ -553,3 +518,4 @@ fn missing_compiler_info() string {
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,34 +1,33 @@
|
||||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
module compiler
|
module compiler
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import strings
|
import strings
|
||||||
|
|
||||||
struct CGen {
|
struct CGen {
|
||||||
out os.File
|
out os.File
|
||||||
out_path string
|
out_path string
|
||||||
//types []string
|
// types []string
|
||||||
thread_fns []string
|
thread_fns []string
|
||||||
//buf strings.Builder
|
// buf strings.Builder
|
||||||
is_user bool
|
is_user bool
|
||||||
mut:
|
mut:
|
||||||
lines []string
|
lines []string
|
||||||
lines_extra []string
|
lines_extra []string
|
||||||
typedefs []string
|
typedefs []string
|
||||||
type_aliases []string
|
type_aliases []string
|
||||||
includes []string
|
includes []string
|
||||||
thread_args []string
|
thread_args []string
|
||||||
consts []string
|
consts []string
|
||||||
const_defines []string
|
const_defines []string
|
||||||
fns []string
|
fns []string
|
||||||
so_fns []string
|
so_fns []string
|
||||||
consts_init []string
|
consts_init []string
|
||||||
pass Pass
|
pass Pass
|
||||||
nogen bool
|
nogen bool
|
||||||
prev_tmps []string
|
prev_tmps []string
|
||||||
tmp_line string
|
tmp_line string
|
||||||
cur_line string
|
cur_line string
|
||||||
prev_line string
|
prev_line string
|
||||||
|
@ -38,19 +37,21 @@ mut:
|
||||||
file string
|
file string
|
||||||
line int
|
line int
|
||||||
line_directives bool
|
line_directives bool
|
||||||
cut_pos int
|
cut_pos int
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_cgen(out_name_c string) &CGen {
|
fn new_cgen(out_name_c string) &CGen {
|
||||||
path := out_name_c
|
path := out_name_c
|
||||||
out := os.create(path) or {
|
out := os.create(path)or{
|
||||||
println('failed to create $path')
|
println('failed to create $path')
|
||||||
return &CGen{}
|
return &CGen{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
gen := &CGen {
|
gen := &CGen{
|
||||||
out_path: path
|
out_path: path
|
||||||
out: out
|
out: out
|
||||||
//buf: strings.new_builder(10000)
|
// buf: strings.new_builder(10000)
|
||||||
|
|
||||||
lines: make(0, 1000, sizeof(string))
|
lines: make(0, 1000, sizeof(string))
|
||||||
}
|
}
|
||||||
return gen
|
return gen
|
||||||
|
@ -118,9 +119,8 @@ fn (g mut CGen) save() {
|
||||||
g.out.close()
|
g.out.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// returns expression's type, and entire expression's string representation)
|
// returns expression's type, and entire expression's string representation)
|
||||||
fn (p mut Parser) tmp_expr() (string, string) {
|
fn (p mut Parser) tmp_expr() (string,string) {
|
||||||
// former start_tmp()
|
// former start_tmp()
|
||||||
if p.cgen.is_tmp {
|
if p.cgen.is_tmp {
|
||||||
p.cgen.prev_tmps << p.cgen.tmp_line
|
p.cgen.prev_tmps << p.cgen.tmp_line
|
||||||
|
@ -130,16 +130,16 @@ fn (p mut Parser) tmp_expr() (string, string) {
|
||||||
p.cgen.is_tmp = true
|
p.cgen.is_tmp = true
|
||||||
//
|
//
|
||||||
typ := p.bool_expression()
|
typ := p.bool_expression()
|
||||||
|
|
||||||
res := p.cgen.tmp_line
|
res := p.cgen.tmp_line
|
||||||
if p.cgen.prev_tmps.len > 0 {
|
if p.cgen.prev_tmps.len > 0 {
|
||||||
p.cgen.tmp_line = p.cgen.prev_tmps.last()
|
p.cgen.tmp_line = p.cgen.prev_tmps.last()
|
||||||
p.cgen.prev_tmps = p.cgen.prev_tmps[0..p.cgen.prev_tmps.len-1]
|
p.cgen.prev_tmps = p.cgen.prev_tmps[0..p.cgen.prev_tmps.len - 1]
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.cgen.tmp_line = ''
|
p.cgen.tmp_line = ''
|
||||||
p.cgen.is_tmp = false
|
p.cgen.is_tmp = false
|
||||||
}
|
}
|
||||||
return typ, res
|
return typ,res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g &CGen) add_placeholder() int {
|
fn (g &CGen) add_placeholder() int {
|
||||||
|
@ -170,10 +170,10 @@ fn (g mut CGen) set_placeholder(pos int, val string) {
|
||||||
if g.nogen || g.pass != .main {
|
if g.nogen || g.pass != .main {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if pos == 0 {
|
// if pos == 0 {
|
||||||
//g.prepend_to_statement(val)
|
// g.prepend_to_statement(val)
|
||||||
//return
|
// return
|
||||||
//}
|
// }
|
||||||
// g.lines.set(pos, val)
|
// g.lines.set(pos, val)
|
||||||
if g.is_tmp {
|
if g.is_tmp {
|
||||||
left := g.tmp_line[..pos]
|
left := g.tmp_line[..pos]
|
||||||
|
@ -208,9 +208,9 @@ fn (g mut CGen) register_thread_fn(wrapper_name, wrapper_text, struct_text strin
|
||||||
fn (v &V) prof_counters() string {
|
fn (v &V) prof_counters() string {
|
||||||
res := []string
|
res := []string
|
||||||
// Global fns
|
// Global fns
|
||||||
//for f in c.table.fns {
|
// for f in c.table.fns {
|
||||||
//res << 'double ${c.table.cgen_name(f)}_time;'
|
// res << 'double ${c.table.cgen_name(f)}_time;'
|
||||||
//}
|
// }
|
||||||
// Methods
|
// Methods
|
||||||
/*
|
/*
|
||||||
for typ in c.table.types {
|
for typ in c.table.types {
|
||||||
|
@ -222,16 +222,17 @@ fn (v &V) prof_counters() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return res.join(';\n')
|
return res.join(';\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p &Parser) print_prof_counters() string {
|
fn (p &Parser) print_prof_counters() string {
|
||||||
res := []string
|
res := []string
|
||||||
// Global fns
|
// Global fns
|
||||||
//for f in p.table.fns {
|
// for f in p.table.fns {
|
||||||
//counter := '${p.table.cgen_name(f)}_time'
|
// counter := '${p.table.cgen_name(f)}_time'
|
||||||
//res << 'if ($counter) printf("%%f : $f.name \\n", $counter);'
|
// res << 'if ($counter) printf("%%f : $f.name \\n", $counter);'
|
||||||
//}
|
// }
|
||||||
// Methods
|
// Methods
|
||||||
/*
|
/*
|
||||||
for typ in p.table.types {
|
for typ in p.table.types {
|
||||||
|
@ -246,6 +247,7 @@ fn (p &Parser) print_prof_counters() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return res.join(';\n')
|
return res.join(';\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,7 +269,6 @@ fn (g mut CGen) add_to_main(s string) {
|
||||||
g.fn_main = g.fn_main + s
|
g.fn_main = g.fn_main + s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
||||||
obj_path := os.realpath(path)
|
obj_path := os.realpath(path)
|
||||||
if os.exists(obj_path) {
|
if os.exists(obj_path) {
|
||||||
|
@ -275,11 +276,13 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
||||||
}
|
}
|
||||||
println('$obj_path not found, building it...')
|
println('$obj_path not found, building it...')
|
||||||
parent := os.dir(obj_path)
|
parent := os.dir(obj_path)
|
||||||
files := os.ls(parent) or { panic(err) }
|
files := os.ls(parent)or{
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
mut cfiles := ''
|
mut cfiles := ''
|
||||||
for file in files {
|
for file in files {
|
||||||
if file.ends_with('.c') {
|
if file.ends_with('.c') {
|
||||||
cfiles += '"' + os.realpath( parent + os.path_separator + file ) + '" '
|
cfiles += '"' + os.realpath(parent + os.path_separator + file) + '" '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cc := find_c_compiler()
|
cc := find_c_compiler()
|
||||||
|
@ -287,7 +290,7 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
||||||
btarget := moduleflags.c_options_before_target()
|
btarget := moduleflags.c_options_before_target()
|
||||||
atarget := moduleflags.c_options_after_target()
|
atarget := moduleflags.c_options_after_target()
|
||||||
cmd := '$cc $cc_thirdparty_options $btarget -c -o "$obj_path" $cfiles $atarget '
|
cmd := '$cc $cc_thirdparty_options $btarget -c -o "$obj_path" $cfiles $atarget '
|
||||||
res := os.exec(cmd) or {
|
res := os.exec(cmd)or{
|
||||||
println('failed thirdparty object build cmd: $cmd')
|
println('failed thirdparty object build cmd: $cmd')
|
||||||
verror(err)
|
verror(err)
|
||||||
return
|
return
|
||||||
|
@ -302,42 +305,82 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
||||||
|
|
||||||
fn os_name_to_ifdef(name string) string {
|
fn os_name_to_ifdef(name string) string {
|
||||||
match name {
|
match name {
|
||||||
'windows' { return '_WIN32' }
|
'windows' {
|
||||||
'mac' { return '__APPLE__' }
|
return '_WIN32'
|
||||||
'macos' { return '__APPLE__' }
|
}
|
||||||
'linux' { return '__linux__' }
|
'mac' {
|
||||||
'freebsd' { return '__FreeBSD__' }
|
return '__APPLE__'
|
||||||
'openbsd'{ return '__OpenBSD__' }
|
}
|
||||||
'netbsd'{ return '__NetBSD__' }
|
'macos' {
|
||||||
'dragonfly'{ return '__DragonFly__' }
|
return '__APPLE__'
|
||||||
'msvc'{ return '_MSC_VER' }
|
}
|
||||||
'android'{ return '__BIONIC__' }
|
'linux' {
|
||||||
'js' {return '_VJS' }
|
return '__linux__'
|
||||||
'solaris'{ return '__sun' }
|
}
|
||||||
'haiku' { return '__haiku__' }
|
'freebsd' {
|
||||||
else { verror('bad os ifdef name "$name"') }
|
return '__FreeBSD__'
|
||||||
}
|
}
|
||||||
//verror('bad os ifdef name "$name"')
|
'openbsd' {
|
||||||
|
return '__OpenBSD__'
|
||||||
|
}
|
||||||
|
'netbsd' {
|
||||||
|
return '__NetBSD__'
|
||||||
|
}
|
||||||
|
'dragonfly' {
|
||||||
|
return '__DragonFly__'
|
||||||
|
}
|
||||||
|
'msvc' {
|
||||||
|
return '_MSC_VER'
|
||||||
|
}
|
||||||
|
'android' {
|
||||||
|
return '__BIONIC__'
|
||||||
|
}
|
||||||
|
'js' {
|
||||||
|
return '_VJS'
|
||||||
|
}
|
||||||
|
'solaris' {
|
||||||
|
return '__sun'
|
||||||
|
}
|
||||||
|
'haiku' {
|
||||||
|
return '__haiku__'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
verror('bad os ifdef name "$name"')
|
||||||
|
}}
|
||||||
|
// verror('bad os ifdef name "$name"')
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
fn platform_postfix_to_ifdefguard(name string) string {
|
fn platform_postfix_to_ifdefguard(name string) string {
|
||||||
s := match name {
|
s := match name {
|
||||||
'.v' { '' }// no guard needed
|
'.v'{
|
||||||
'_win.v', '_windows.v' { '#ifdef _WIN32' }
|
|
||||||
'_nix.v' { '#ifndef _WIN32' }
|
|
||||||
'_lin.v', '_linux.v' { '#ifdef __linux__' }
|
|
||||||
'_mac.v', '_darwin.v' { '#ifdef __APPLE__' }
|
|
||||||
'_bsd.v', '_freebsd.v '{ '#ifdef __FreeBSD__'}
|
|
||||||
'_solaris.v' { '#ifdef __sun' }
|
|
||||||
'_haiku.v' { '#ifdef __haiku__' }
|
|
||||||
else {
|
|
||||||
|
|
||||||
//verror('bad platform_postfix "$name"')
|
|
||||||
// TODO
|
|
||||||
''
|
''
|
||||||
|
} // no guard needed
|
||||||
|
'_win.v','_windows.v'{
|
||||||
|
'#ifdef _WIN32'
|
||||||
}
|
}
|
||||||
}
|
'_nix.v'{
|
||||||
|
'#ifndef _WIN32'
|
||||||
|
}
|
||||||
|
'_lin.v','_linux.v'{
|
||||||
|
'#ifdef __linux__'
|
||||||
|
}
|
||||||
|
'_mac.v','_darwin.v'{
|
||||||
|
'#ifdef __APPLE__'
|
||||||
|
}
|
||||||
|
'_bsd.v','_freebsd.v '{
|
||||||
|
'#ifdef __FreeBSD__'
|
||||||
|
}
|
||||||
|
'_solaris.v'{
|
||||||
|
'#ifdef __sun'
|
||||||
|
}
|
||||||
|
'_haiku.v'{
|
||||||
|
'#ifdef __haiku__'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// verror('bad platform_postfix "$name"')
|
||||||
|
// TODO
|
||||||
|
''}}
|
||||||
if s == '' {
|
if s == '' {
|
||||||
verror('bad platform_postfix "$name"')
|
verror('bad platform_postfix "$name"')
|
||||||
}
|
}
|
||||||
|
@ -366,8 +409,7 @@ fn (v &V) type_definitions() string {
|
||||||
// sort structs
|
// sort structs
|
||||||
types_sorted := sort_structs(types)
|
types_sorted := sort_structs(types)
|
||||||
// Generate C code
|
// Generate C code
|
||||||
res := types_to_c(builtin_types,v.table) + '\n//----\n' +
|
res := types_to_c(builtin_types, v.table) + '\n//----\n' + types_to_c(types_sorted, v.table)
|
||||||
types_to_c(types_sorted, v.table)
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -385,13 +427,12 @@ fn sort_structs(types []Type) []Type {
|
||||||
mut field_deps := []string
|
mut field_deps := []string
|
||||||
for field in t.fields {
|
for field in t.fields {
|
||||||
// Need to handle fixed size arrays as well (`[10]Point`)
|
// Need to handle fixed size arrays as well (`[10]Point`)
|
||||||
ft := if field.typ.starts_with('[') { field.typ.all_after(']') }
|
ft := if field.typ.starts_with('[') { field.typ.all_after(']') } else { field.typ }
|
||||||
else { field.typ }
|
|
||||||
// skip if not in types list or already in deps
|
// skip if not in types list or already in deps
|
||||||
if !(ft in type_names) || ft in field_deps {
|
if !(ft in type_names) || ft in field_deps {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
field_deps << ft//field.typ
|
field_deps << ft // field.typ
|
||||||
}
|
}
|
||||||
// add type and dependant types to graph
|
// add type and dependant types to graph
|
||||||
dep_graph.add(t.name, field_deps)
|
dep_graph.add(t.name, field_deps)
|
||||||
|
@ -399,10 +440,7 @@ fn sort_structs(types []Type) []Type {
|
||||||
// sort graph
|
// sort graph
|
||||||
dep_graph_sorted := dep_graph.resolve()
|
dep_graph_sorted := dep_graph.resolve()
|
||||||
if !dep_graph_sorted.acyclic {
|
if !dep_graph_sorted.acyclic {
|
||||||
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' +
|
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + dep_graph_sorted.display_cycles() + '\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' + '\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
||||||
dep_graph_sorted.display_cycles() +
|
|
||||||
'\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' +
|
|
||||||
'\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
|
||||||
}
|
}
|
||||||
// sort types
|
// sort types
|
||||||
mut types_sorted := []Type
|
mut types_sorted := []Type
|
||||||
|
@ -419,35 +457,33 @@ fn sort_structs(types []Type) []Type {
|
||||||
|
|
||||||
// Generates interface table and interface indexes
|
// Generates interface table and interface indexes
|
||||||
fn (v &V) interface_table() string {
|
fn (v &V) interface_table() string {
|
||||||
mut sb := strings.new_builder(100)
|
mut sb := strings.new_builder(100)
|
||||||
for _, t in v.table.typesmap {
|
for _, t in v.table.typesmap {
|
||||||
if t.cat != .interface_ {
|
if t.cat != .interface_ {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mut methods := ''
|
mut methods := ''
|
||||||
sb.writeln('// NR methods = $t.gen_types.len')
|
sb.writeln('// NR methods = $t.gen_types.len')
|
||||||
for i, gen_type in t.gen_types {
|
for i, gen_type in t.gen_types {
|
||||||
methods += '{'
|
methods += '{'
|
||||||
for j, method in t.methods {
|
for j, method in t.methods {
|
||||||
// Cat_speak
|
// Cat_speak
|
||||||
methods += '${gen_type}_${method.name}'
|
methods += '${gen_type}_${method.name}'
|
||||||
if j < t.methods.len - 1 {
|
if j < t.methods.len - 1 {
|
||||||
methods += ', '
|
methods += ', '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
methods += '}, '
|
methods += '}, '
|
||||||
// Speaker_Cat_index = 0
|
// Speaker_Cat_index = 0
|
||||||
sb.writeln('int _${t.name}_${gen_type}_index = $i;')
|
sb.writeln('int _${t.name}_${gen_type}_index = $i;')
|
||||||
}
|
}
|
||||||
if t.gen_types.len > 0 {
|
if t.gen_types.len > 0 {
|
||||||
// methods = '{TCCSKIP(0)}'
|
// methods = '{TCCSKIP(0)}'
|
||||||
// }
|
// }
|
||||||
sb.writeln('void* (* ${t.name}_name_table[][$t.methods.len]) = ' +
|
sb.writeln('void* (* ${t.name}_name_table[][$t.methods.len]) = ' + '{ $methods }; ')
|
||||||
'{ $methods }; ')
|
}
|
||||||
}
|
continue
|
||||||
continue
|
}
|
||||||
}
|
return sb.str()
|
||||||
return sb.str()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
module compiler
|
module compiler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
vweb.tmpl // for `$vweb_html()`
|
vweb.tmpl // for `$vweb_html()`
|
||||||
os
|
os
|
||||||
strings
|
strings
|
||||||
)
|
)
|
||||||
|
@ -34,10 +33,7 @@ fn (p mut Parser) comp_time() {
|
||||||
}
|
}
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
os := os_from_string(name)
|
os := os_from_string(name)
|
||||||
if ((!not && os != p.os) || (not && os == p.os)) &&
|
if ((!not && os != p.os) || (not && os == p.os)) && !p.scanner.is_fmt && !p.pref.output_cross_c {
|
||||||
!p.scanner.is_fmt &&
|
|
||||||
!p.pref.output_cross_c
|
|
||||||
{
|
|
||||||
// `$if os {` for a different target, skip everything inside
|
// `$if os {` for a different target, skip everything inside
|
||||||
// to avoid compilation errors (like including <windows.h>
|
// to avoid compilation errors (like including <windows.h>
|
||||||
// on non-Windows systems)
|
// on non-Windows systems)
|
||||||
|
@ -48,23 +44,25 @@ fn (p mut Parser) comp_time() {
|
||||||
}
|
}
|
||||||
if p.tok == .lcbr {
|
if p.tok == .lcbr {
|
||||||
stack++
|
stack++
|
||||||
} else if p.tok == .rcbr {
|
}
|
||||||
|
else if p.tok == .rcbr {
|
||||||
stack--
|
stack--
|
||||||
}
|
}
|
||||||
if p.tok == .eof {
|
if p.tok == .eof {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if stack <= 0 && p.tok == .rcbr {
|
if stack <= 0 && p.tok == .rcbr {
|
||||||
//p.warn('exiting $stack')
|
// p.warn('exiting $stack')
|
||||||
p.next()
|
p.next()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.statements_no_rcbr()
|
p.statements_no_rcbr()
|
||||||
}
|
}
|
||||||
if ! (p.tok == .dollar && p.peek() == .key_else) {
|
if !(p.tok == .dollar && p.peek() == .key_else) {
|
||||||
p.genln('#endif')
|
p.genln('#endif')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,7 +106,7 @@ fn (p mut Parser) comp_time() {
|
||||||
}
|
}
|
||||||
if_returns := p.returns
|
if_returns := p.returns
|
||||||
p.returns = false
|
p.returns = false
|
||||||
//p.gen('/* returns $p.returns */')
|
// p.gen('/* returns $p.returns */')
|
||||||
if p.tok == .dollar && p.peek() == .key_else {
|
if p.tok == .dollar && p.peek() == .key_else {
|
||||||
p.fspace()
|
p.fspace()
|
||||||
p.next()
|
p.next()
|
||||||
|
@ -120,8 +118,9 @@ fn (p mut Parser) comp_time() {
|
||||||
p.genln('#endif')
|
p.genln('#endif')
|
||||||
else_returns := p.returns
|
else_returns := p.returns
|
||||||
p.returns = if_returns && else_returns
|
p.returns = if_returns && else_returns
|
||||||
//p.gen('/* returns $p.returns */')
|
// p.gen('/* returns $p.returns */')
|
||||||
} else if p.tok == .key_else {
|
}
|
||||||
|
else if p.tok == .key_else {
|
||||||
p.error('use `$' + 'else` instead of `else` in comptime if statements')
|
p.error('use `$' + 'else` instead of `else` in comptime if statements')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +133,7 @@ fn (p mut Parser) comp_time() {
|
||||||
p.check(.key_in)
|
p.check(.key_in)
|
||||||
p.check_name()
|
p.check_name()
|
||||||
p.check(.dot)
|
p.check(.dot)
|
||||||
p.check_name()// fields
|
p.check_name() // fields
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
// for p.tok != .rcbr && p.tok != .eof {
|
// for p.tok != .rcbr && p.tok != .eof {
|
||||||
res_name := p.check_name()
|
res_name := p.check_name()
|
||||||
|
@ -143,9 +142,9 @@ fn (p mut Parser) comp_time() {
|
||||||
p.check(.dollar)
|
p.check(.dollar)
|
||||||
p.check(.name)
|
p.check(.name)
|
||||||
p.check(.assign)
|
p.check(.assign)
|
||||||
_, val := p.tmp_expr()
|
_,val := p.tmp_expr()
|
||||||
//p.bool_expression()
|
// p.bool_expression()
|
||||||
//val := p.cgen.end_tmp()
|
// val := p.cgen.end_tmp()
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
@ -166,7 +165,7 @@ fn (p mut Parser) comp_time() {
|
||||||
p.error('vweb HTML template "$path" not found')
|
p.error('vweb HTML template "$path" not found')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.check(.name) // skip `vweb.html()` TODO
|
p.check(.name) // skip `vweb.html()` TODO
|
||||||
p.check(.dot)
|
p.check(.dot)
|
||||||
p.check(.name)
|
p.check(.name)
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
|
@ -209,9 +208,9 @@ fn (p mut Parser) chash() {
|
||||||
flag = flag.replace('@VPATH', p.pref.vpath)
|
flag = flag.replace('@VPATH', p.pref.vpath)
|
||||||
flag = flag.replace('@VLIB_PATH', p.pref.vlib_path)
|
flag = flag.replace('@VLIB_PATH', p.pref.vlib_path)
|
||||||
flag = flag.replace('@VMOD', v_modules_path)
|
flag = flag.replace('@VMOD', v_modules_path)
|
||||||
//p.log('adding flag "$flag"')
|
// p.log('adding flag "$flag"')
|
||||||
_ = p.table.parse_cflag(flag, p.mod) or {
|
_ = p.table.parse_cflag(flag, p.mod) or {
|
||||||
p.error_with_token_index(err, p.cur_tok_index()-1)
|
p.error_with_token_index(err, p.cur_tok_index() - 1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -226,7 +225,7 @@ fn (p mut Parser) chash() {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
if p.file_pcguard.len != 0 {
|
if p.file_pcguard.len != 0 {
|
||||||
//println('p: $p.file_platform $p.file_pcguard')
|
// println('p: $p.file_platform $p.file_pcguard')
|
||||||
p.cgen.includes << '$p.file_pcguard\n#$hash\n#endif'
|
p.cgen.includes << '$p.file_pcguard\n#$hash\n#endif'
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -236,11 +235,13 @@ fn (p mut Parser) chash() {
|
||||||
}
|
}
|
||||||
// TODO remove after ui_mac.m is removed
|
// TODO remove after ui_mac.m is removed
|
||||||
else if hash.contains('embed') {
|
else if hash.contains('embed') {
|
||||||
pos := hash.index('embed') or { return }
|
pos := hash.index('embed') or {
|
||||||
file := hash[pos+5..]
|
return
|
||||||
//if p.pref.build_mode != .default_mode {
|
}
|
||||||
p.genln('#include $file')
|
file := hash[pos + 5..]
|
||||||
//}
|
// if p.pref.build_mode != .default_mode {
|
||||||
|
p.genln('#include $file')
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
else if hash.contains('define') {
|
else if hash.contains('define') {
|
||||||
// Move defines on top
|
// Move defines on top
|
||||||
|
@ -248,8 +249,8 @@ fn (p mut Parser) chash() {
|
||||||
p.cgen.includes << '#$hash'
|
p.cgen.includes << '#$hash'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//// Don't parse a non-JS V file (`#-js` flag)
|
// // Don't parse a non-JS V file (`#-js` flag)
|
||||||
else if hash == '-js' {
|
else if hash == '-js' {
|
||||||
$if js {
|
$if js {
|
||||||
for p.tok != .eof {
|
for p.tok != .eof {
|
||||||
p.next()
|
p.next()
|
||||||
|
@ -262,7 +263,11 @@ fn (p mut Parser) chash() {
|
||||||
$if !js {
|
$if !js {
|
||||||
if !p.can_chash {
|
if !p.can_chash {
|
||||||
println('hash="$hash"')
|
println('hash="$hash"')
|
||||||
if hash.starts_with('include') { println("include") } else {}
|
if hash.starts_with('include') {
|
||||||
|
println('include')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
}
|
||||||
p.error('bad token `#` (embedding C code is no longer supported)')
|
p.error('bad token `#` (embedding C code is no longer supported)')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -278,7 +283,6 @@ fn (p mut Parser) comptime_method_call(typ Type) {
|
||||||
mut j := 0
|
mut j := 0
|
||||||
for method in typ.methods {
|
for method in typ.methods {
|
||||||
if method.typ != 'void' {
|
if method.typ != 'void' {
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
receiver := method.args[0]
|
receiver := method.args[0]
|
||||||
|
@ -289,8 +293,7 @@ fn (p mut Parser) comptime_method_call(typ Type) {
|
||||||
if j > 0 {
|
if j > 0 {
|
||||||
p.gen(' else ')
|
p.gen(' else ')
|
||||||
}
|
}
|
||||||
p.genln('if ( string_eq($var, _STR("$method.name")) ) ' +
|
p.genln('if ( string_eq($var, _STR("$method.name")) ) ' + '${typ.name}_$method.name ($amp $p.expr_var.name);')
|
||||||
'${typ.name}_$method.name ($amp $p.expr_var.name);')
|
|
||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
|
@ -310,7 +313,10 @@ fn (p mut Parser) gen_array_str(typ Type) {
|
||||||
p.add_method(typ.name, Fn{
|
p.add_method(typ.name, Fn{
|
||||||
name: 'str'
|
name: 'str'
|
||||||
typ: 'string'
|
typ: 'string'
|
||||||
args: [Var{typ: typ.name, is_arg:true}]
|
args: [Var{
|
||||||
|
typ: typ.name
|
||||||
|
is_arg: true
|
||||||
|
}]
|
||||||
is_method: true
|
is_method: true
|
||||||
is_public: true
|
is_public: true
|
||||||
receiver_typ: typ.name
|
receiver_typ: typ.name
|
||||||
|
@ -320,8 +326,8 @@ fn (p mut Parser) gen_array_str(typ Type) {
|
||||||
is_array := elm_type.starts_with('array_')
|
is_array := elm_type.starts_with('array_')
|
||||||
if is_array {
|
if is_array {
|
||||||
p.gen_array_str(elm_type2)
|
p.gen_array_str(elm_type2)
|
||||||
} else if p.typ_to_fmt(elm_type, 0) == '' &&
|
}
|
||||||
!p.table.type_has_method(elm_type2, 'str') {
|
else if p.typ_to_fmt(elm_type, 0) == '' && !p.table.type_has_method(elm_type2, 'str') {
|
||||||
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
||||||
}
|
}
|
||||||
p.v.vgen_buf.writeln('
|
p.v.vgen_buf.writeln('
|
||||||
|
@ -346,12 +352,14 @@ fn (p mut Parser) gen_struct_str(typ Type) {
|
||||||
p.add_method(typ.name, Fn{
|
p.add_method(typ.name, Fn{
|
||||||
name: 'str'
|
name: 'str'
|
||||||
typ: 'string'
|
typ: 'string'
|
||||||
args: [Var{typ: typ.name, is_arg:true}]
|
args: [Var{
|
||||||
|
typ: typ.name
|
||||||
|
is_arg: true
|
||||||
|
}]
|
||||||
is_method: true
|
is_method: true
|
||||||
is_public: true
|
is_public: true
|
||||||
receiver_typ: typ.name
|
receiver_typ: typ.name
|
||||||
})
|
})
|
||||||
|
|
||||||
mut sb := strings.new_builder(typ.fields.len * 20)
|
mut sb := strings.new_builder(typ.fields.len * 20)
|
||||||
sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
|
sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
|
||||||
sb.writeln("'{")
|
sb.writeln("'{")
|
||||||
|
@ -373,7 +381,8 @@ fn (p mut Parser) gen_varg_str(typ Type) {
|
||||||
is_array := elm_type.starts_with('array_')
|
is_array := elm_type.starts_with('array_')
|
||||||
if is_array {
|
if is_array {
|
||||||
p.gen_array_str(elm_type2)
|
p.gen_array_str(elm_type2)
|
||||||
} else if elm_type2.cat == .struct_ {
|
}
|
||||||
|
else if elm_type2.cat == .struct_ {
|
||||||
p.gen_struct_str(elm_type2)
|
p.gen_struct_str(elm_type2)
|
||||||
}
|
}
|
||||||
p.v.vgen_buf.writeln('
|
p.v.vgen_buf.writeln('
|
||||||
|
@ -407,7 +416,7 @@ fn (p mut Parser) gen_array_filter(str_typ string, method_ph int) {
|
||||||
}
|
}
|
||||||
array_int b = tmp2;
|
array_int b = tmp2;
|
||||||
*/
|
*/
|
||||||
val_type:=str_typ[6..]
|
val_type := str_typ[6..]
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
p.register_var(Var{
|
p.register_var(Var{
|
||||||
name: 'it'
|
name: 'it'
|
||||||
|
@ -418,14 +427,14 @@ fn (p mut Parser) gen_array_filter(str_typ string, method_ph int) {
|
||||||
p.cgen.resetln('')
|
p.cgen.resetln('')
|
||||||
tmp := p.get_tmp()
|
tmp := p.get_tmp()
|
||||||
a := p.expr_var.name
|
a := p.expr_var.name
|
||||||
p.cgen.set_placeholder(method_ph,'\n$str_typ $tmp = new_array(0, $a .len,sizeof($val_type));\n')
|
p.cgen.set_placeholder(method_ph, '\n$str_typ $tmp = new_array(0, $a .len,sizeof($val_type));\n')
|
||||||
p.genln('for (int i = 0; i < ${a}.len; i++) {')
|
p.genln('for (int i = 0; i < ${a}.len; i++) {')
|
||||||
p.genln('$val_type it = (($val_type*)${a}.data)[i];')
|
p.genln('$val_type it = (($val_type*)${a}.data)[i];')
|
||||||
p.gen('if (')
|
p.gen('if (')
|
||||||
p.bool_expression()
|
p.bool_expression()
|
||||||
p.genln(') array_push(&$tmp, &it);')
|
p.genln(') array_push(&$tmp, &it);')
|
||||||
//p.genln(') array_push(&$tmp, &((($val_type*)${a}.data)[i]));')
|
// p.genln(') array_push(&$tmp, &((($val_type*)${a}.data)[i]));')
|
||||||
//p.genln(') array_push(&$tmp, ${a}.data + i * ${a}.element_size);')
|
// p.genln(') array_push(&$tmp, ${a}.data + i * ${a}.element_size);')
|
||||||
p.genln('}')
|
p.genln('}')
|
||||||
p.gen(tmp) // TODO why does this `gen()` work?
|
p.gen(tmp) // TODO why does this `gen()` work?
|
||||||
p.check(.rpar)
|
p.check(.rpar)
|
||||||
|
@ -447,7 +456,7 @@ fn (p mut Parser) gen_array_map(str_typ string, method_ph int) string {
|
||||||
}
|
}
|
||||||
array_int b = tmp2;
|
array_int b = tmp2;
|
||||||
*/
|
*/
|
||||||
val_type:=str_typ[6..]
|
val_type := str_typ[6..]
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
p.register_var(Var{
|
p.register_var(Var{
|
||||||
name: 'it'
|
name: 'it'
|
||||||
|
@ -459,9 +468,8 @@ fn (p mut Parser) gen_array_map(str_typ string, method_ph int) string {
|
||||||
tmp := p.get_tmp()
|
tmp := p.get_tmp()
|
||||||
tmp_elm := p.get_tmp()
|
tmp_elm := p.get_tmp()
|
||||||
a := p.expr_var.name
|
a := p.expr_var.name
|
||||||
map_type, expr := p.tmp_expr()
|
map_type,expr := p.tmp_expr()
|
||||||
p.cgen.set_placeholder(method_ph,'\narray $tmp = new_array(0, $a .len, ' +
|
p.cgen.set_placeholder(method_ph, '\narray $tmp = new_array(0, $a .len, ' + 'sizeof($map_type));\n')
|
||||||
'sizeof($map_type));\n')
|
|
||||||
p.genln('for (int i = 0; i < ${a}.len; i++) {')
|
p.genln('for (int i = 0; i < ${a}.len; i++) {')
|
||||||
p.genln('$val_type it = (($val_type*)${a}.data)[i];')
|
p.genln('$val_type it = (($val_type*)${a}.data)[i];')
|
||||||
p.genln('_PUSH(&$tmp, $expr, $tmp_elm, $map_type)')
|
p.genln('_PUSH(&$tmp, $expr, $tmp_elm, $map_type)')
|
||||||
|
@ -476,7 +484,7 @@ fn (p mut Parser) comptime_if_block(name string) {
|
||||||
p.genln('#ifdef $name')
|
p.genln('#ifdef $name')
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
p.statements_no_rcbr()
|
p.statements_no_rcbr()
|
||||||
if ! (p.tok == .dollar && p.peek() == .key_else) {
|
if !(p.tok == .dollar && p.peek() == .key_else) {
|
||||||
p.genln('#endif')
|
p.genln('#endif')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -484,9 +492,16 @@ fn (p mut Parser) comptime_if_block(name string) {
|
||||||
fn (p mut Parser) gen_enum_flag_methods(typ mut Type) {
|
fn (p mut Parser) gen_enum_flag_methods(typ mut Type) {
|
||||||
for method in ['set', 'clear', 'toggle', 'has'] {
|
for method in ['set', 'clear', 'toggle', 'has'] {
|
||||||
typ.methods << Fn{
|
typ.methods << Fn{
|
||||||
name: method,
|
name: method
|
||||||
typ: if method == 'has' { 'bool' } else { 'void' }
|
typ: if method == 'has' { 'bool' } else { 'void' }
|
||||||
args: [Var{typ: typ.name, is_mut: true, is_arg:true}, Var{typ: typ.name, is_arg: true}]
|
args: [Var{
|
||||||
|
typ: typ.name
|
||||||
|
is_mut: true
|
||||||
|
is_arg: true
|
||||||
|
}, Var{
|
||||||
|
typ: typ.name
|
||||||
|
is_arg: true
|
||||||
|
}]
|
||||||
is_method: true
|
is_method: true
|
||||||
is_public: true
|
is_public: true
|
||||||
receiver_typ: typ.name
|
receiver_typ: typ.name
|
||||||
|
@ -496,10 +511,10 @@ fn (p mut Parser) gen_enum_flag_methods(typ mut Type) {
|
||||||
pub fn (e mut $typ.name) set(flag $typ.name) { *e = int(*e) | (1 << int(flag)) }
|
pub fn (e mut $typ.name) set(flag $typ.name) { *e = int(*e) | (1 << int(flag)) }
|
||||||
pub fn (e mut $typ.name) clear(flag $typ.name) { *e = int(*e) &~ (1 << int(flag)) }
|
pub fn (e mut $typ.name) clear(flag $typ.name) { *e = int(*e) &~ (1 << int(flag)) }
|
||||||
pub fn (e mut $typ.name) toggle(flag $typ.name) { *e = int(*e) ^ (1 << int(flag)) }
|
pub fn (e mut $typ.name) toggle(flag $typ.name) { *e = int(*e) ^ (1 << int(flag)) }
|
||||||
pub fn (e &$typ.name) has(flag $typ.name) bool { return int(*e)&(1 << int(flag)) != 0 }'
|
pub fn (e &$typ.name) has(flag $typ.name) bool { return int(*e)&(1 << int(flag)) != 0 }')
|
||||||
)
|
|
||||||
p.cgen.fns << 'void ${typ.name}_set($typ.name *e, $typ.name flag);'
|
p.cgen.fns << 'void ${typ.name}_set($typ.name *e, $typ.name flag);'
|
||||||
p.cgen.fns << 'void ${typ.name}_clear($typ.name *e, $typ.name flag);'
|
p.cgen.fns << 'void ${typ.name}_clear($typ.name *e, $typ.name flag);'
|
||||||
p.cgen.fns << 'void ${typ.name}_toggle($typ.name *e, $typ.name flag);'
|
p.cgen.fns << 'void ${typ.name}_toggle($typ.name *e, $typ.name flag);'
|
||||||
p.cgen.fns << 'bool ${typ.name}_has($typ.name *e, $typ.name flag);'
|
p.cgen.fns << 'bool ${typ.name}_has($typ.name *e, $typ.name flag);'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,28 @@
|
||||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
module compiler
|
module compiler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
strings
|
strings
|
||||||
)
|
)
|
||||||
|
|
||||||
// Returns type if used as expression
|
// Returns type if used as expression
|
||||||
|
|
||||||
|
|
||||||
fn (p mut Parser) match_statement(is_expr bool) string {
|
fn (p mut Parser) match_statement(is_expr bool) string {
|
||||||
p.check(.key_match)
|
p.check(.key_match)
|
||||||
p.fspace()
|
p.fspace()
|
||||||
typ, expr := p.tmp_expr()
|
typ,expr := p.tmp_expr()
|
||||||
if typ.starts_with('array_') {
|
if typ.starts_with('array_') {
|
||||||
p.error('arrays cannot be compared')
|
p.error('arrays cannot be compared')
|
||||||
}
|
}
|
||||||
// is it safe to use p.cgen.insert_before ???
|
// is it safe to use p.cgen.insert_before ???
|
||||||
tmp_var := p.get_tmp()
|
tmp_var := p.get_tmp()
|
||||||
p.cgen.insert_before('$typ $tmp_var = $expr;')
|
p.cgen.insert_before('$typ $tmp_var = $expr;')
|
||||||
|
|
||||||
p.fspace()
|
p.fspace()
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
mut i := 0
|
mut i := 0
|
||||||
mut all_cases_return := true
|
mut all_cases_return := true
|
||||||
|
|
||||||
// stores typ of resulting variable
|
// stores typ of resulting variable
|
||||||
mut res_typ := ''
|
mut res_typ := ''
|
||||||
defer {
|
defer {
|
||||||
|
@ -36,108 +34,89 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||||
if p.tok == .arrow {
|
if p.tok == .arrow {
|
||||||
p.error(warn_match_arrow)
|
p.error(warn_match_arrow)
|
||||||
}
|
}
|
||||||
|
|
||||||
// unwrap match if there is only else
|
// unwrap match if there is only else
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
p.fspace()
|
p.fspace()
|
||||||
if is_expr {
|
if is_expr {
|
||||||
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
||||||
|
|
||||||
// allow braces is else
|
// allow braces is else
|
||||||
got_brace := p.tok == .lcbr
|
got_brace := p.tok == .lcbr
|
||||||
if got_brace {
|
if got_brace {
|
||||||
p.fspace()
|
p.fspace()
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.gen('( ')
|
p.gen('( ')
|
||||||
|
|
||||||
res_typ = p.bool_expression()
|
res_typ = p.bool_expression()
|
||||||
|
|
||||||
p.gen(' )')
|
p.gen(' )')
|
||||||
|
|
||||||
// allow braces in else
|
// allow braces in else
|
||||||
if got_brace {
|
if got_brace {
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res_typ
|
return res_typ
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.returns = false
|
p.returns = false
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
|
|
||||||
p.genln('{ ')
|
p.genln('{ ')
|
||||||
p.statements()
|
p.statements()
|
||||||
p.returns = all_cases_return && p.returns
|
p.returns = all_cases_return && p.returns
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_expr {
|
if is_expr {
|
||||||
// statements are dissallowed (if match is expression) so
|
// statements are dissallowed (if match is expression) so
|
||||||
// user cant declare variables there and so on
|
// user cant declare variables there and so on
|
||||||
p.gen(':(')
|
p.gen(':(')
|
||||||
|
|
||||||
// allow braces is else
|
// allow braces is else
|
||||||
got_brace := p.tok == .lcbr
|
got_brace := p.tok == .lcbr
|
||||||
if got_brace {
|
if got_brace {
|
||||||
p.fspace()
|
p.fspace()
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.check_types(p.bool_expression(), res_typ)
|
p.check_types(p.bool_expression(), res_typ)
|
||||||
|
|
||||||
// allow braces in else
|
// allow braces in else
|
||||||
if got_brace {
|
if got_brace {
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
}
|
}
|
||||||
|
p.gen(strings.repeat(`)`, i + 1))
|
||||||
p.gen(strings.repeat(`)`, i+1))
|
|
||||||
|
|
||||||
return res_typ
|
return res_typ
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.returns = false
|
p.returns = false
|
||||||
p.genln('else // default:')
|
p.genln('else // default:')
|
||||||
|
|
||||||
p.fspace()
|
p.fspace()
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
|
|
||||||
p.genln('{ ')
|
p.genln('{ ')
|
||||||
p.statements()
|
p.statements()
|
||||||
|
|
||||||
p.returns = all_cases_return && p.returns
|
p.returns = all_cases_return && p.returns
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
if is_expr {
|
if is_expr {
|
||||||
p.gen(': (')
|
p.gen(': (')
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.gen('else ')
|
p.gen('else ')
|
||||||
}
|
}
|
||||||
} else if is_expr {
|
}
|
||||||
|
else if is_expr {
|
||||||
p.gen('(')
|
p.gen('(')
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_expr {
|
if is_expr {
|
||||||
p.gen('(')
|
p.gen('(')
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.gen('if (')
|
p.gen('if (')
|
||||||
}
|
}
|
||||||
|
|
||||||
ph := p.cgen.add_placeholder()
|
ph := p.cgen.add_placeholder()
|
||||||
|
|
||||||
// Multiple checks separated by comma
|
// Multiple checks separated by comma
|
||||||
mut got_comma := false
|
mut got_comma := false
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if got_comma {
|
if got_comma {
|
||||||
p.gen(') || (')
|
p.gen(') || (')
|
||||||
}
|
}
|
||||||
|
|
||||||
mut got_string := false
|
mut got_string := false
|
||||||
|
|
||||||
if typ == 'string' {
|
if typ == 'string' {
|
||||||
got_string = true
|
got_string = true
|
||||||
p.gen('string_eq($tmp_var, ')
|
p.gen('string_eq($tmp_var, ')
|
||||||
|
@ -145,15 +124,12 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||||
else {
|
else {
|
||||||
p.gen('$tmp_var == ')
|
p.gen('$tmp_var == ')
|
||||||
}
|
}
|
||||||
|
|
||||||
p.expected_type = typ
|
p.expected_type = typ
|
||||||
p.check_types(p.bool_expression(), typ)
|
p.check_types(p.bool_expression(), typ)
|
||||||
p.expected_type = ''
|
p.expected_type = ''
|
||||||
|
|
||||||
if got_string {
|
if got_string {
|
||||||
p.gen(')')
|
p.gen(')')
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.tok != .comma {
|
if p.tok != .comma {
|
||||||
if got_comma {
|
if got_comma {
|
||||||
p.gen(') ')
|
p.gen(') ')
|
||||||
|
@ -165,41 +141,34 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||||
got_comma = true
|
got_comma = true
|
||||||
}
|
}
|
||||||
p.gen(')')
|
p.gen(')')
|
||||||
|
|
||||||
if p.tok == .arrow {
|
if p.tok == .arrow {
|
||||||
p.error(warn_match_arrow)
|
p.error(warn_match_arrow)
|
||||||
p.check(.arrow)
|
p.check(.arrow)
|
||||||
}
|
}
|
||||||
|
|
||||||
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
||||||
if is_expr {
|
if is_expr {
|
||||||
p.gen('? (')
|
p.gen('? (')
|
||||||
|
|
||||||
// braces are required for now
|
// braces are required for now
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
|
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
// on the first iteration we set value of res_typ
|
// on the first iteration we set value of res_typ
|
||||||
res_typ = p.bool_expression()
|
res_typ = p.bool_expression()
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
// later on we check that the value is of res_typ type
|
// later on we check that the value is of res_typ type
|
||||||
p.check_types(p.bool_expression(), res_typ)
|
p.check_types(p.bool_expression(), res_typ)
|
||||||
}
|
}
|
||||||
|
|
||||||
// braces are required for now
|
// braces are required for now
|
||||||
p.fgen_nl()
|
p.fgen_nl()
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
|
|
||||||
p.gen(')')
|
p.gen(')')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
p.returns = false
|
p.returns = false
|
||||||
p.fspace()
|
p.fspace()
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
|
|
||||||
p.genln('{ ')
|
p.genln('{ ')
|
||||||
p.statements()
|
p.statements()
|
||||||
|
|
||||||
all_cases_return = all_cases_return && p.returns
|
all_cases_return = all_cases_return && p.returns
|
||||||
// p.gen(')')
|
// p.gen(')')
|
||||||
}
|
}
|
||||||
|
@ -207,20 +176,19 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||||
p.fgen_nl()
|
p.fgen_nl()
|
||||||
}
|
}
|
||||||
p.error('match must be exhaustive')
|
p.error('match must be exhaustive')
|
||||||
//p.returns = false // only get here when no default, so return is not guaranteed
|
// p.returns = false // only get here when no default, so return is not guaranteed
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) switch_statement() {
|
fn (p mut Parser) switch_statement() {
|
||||||
p.error('`switch` statement has been removed, use `match` instead:\n' +
|
p.error('`switch` statement has been removed, use `match` instead:\n' + 'https://vlang.io/docs#match')
|
||||||
'https://vlang.io/docs#match')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
||||||
if is_expr {
|
if is_expr {
|
||||||
//if p.fileis('if_expr') {
|
// if p.fileis('if_expr') {
|
||||||
//println('IF EXPR')
|
// println('IF EXPR')
|
||||||
//}
|
// }
|
||||||
p.inside_if_expr = true
|
p.inside_if_expr = true
|
||||||
p.gen('((')
|
p.gen('((')
|
||||||
}
|
}
|
||||||
|
@ -241,7 +209,7 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
||||||
p.next()
|
p.next()
|
||||||
p.check(.decl_assign)
|
p.check(.decl_assign)
|
||||||
p.is_var_decl = true
|
p.is_var_decl = true
|
||||||
option_type, expr := p.tmp_expr()// := p.bool_expression()
|
option_type,expr := p.tmp_expr() // := p.bool_expression()
|
||||||
if !option_type.starts_with('Option_') {
|
if !option_type.starts_with('Option_') {
|
||||||
p.error('`if x := opt() {` syntax requires a function that returns an optional value')
|
p.error('`if x := opt() {` syntax requires a function that returns an optional value')
|
||||||
}
|
}
|
||||||
|
@ -249,27 +217,30 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
||||||
typ := option_type[7..]
|
typ := option_type[7..]
|
||||||
// Option_User tmp = get_user(1);
|
// Option_User tmp = get_user(1);
|
||||||
// if (tmp.ok) {
|
// if (tmp.ok) {
|
||||||
// User user = *(User*)tmp.data;
|
// User user = *(User*)tmp.data;
|
||||||
// [statements]
|
// [statements]
|
||||||
// }
|
// }
|
||||||
p.cgen.insert_before('$option_type $option_tmp = $expr; ')
|
p.cgen.insert_before('$option_type $option_tmp = $expr; ')
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
p.genln(option_tmp + '.ok) {')
|
p.genln(option_tmp + '.ok) {')
|
||||||
p.genln('$typ $var_name = *($typ*) $option_tmp . data;')
|
p.genln('$typ $var_name = *($typ*) $option_tmp . data;')
|
||||||
p.register_var(Var {
|
p.register_var(Var{
|
||||||
name: var_name
|
name: var_name
|
||||||
typ: typ
|
typ: typ
|
||||||
is_mut: false // TODO
|
is_mut: false // TODO
|
||||||
|
|
||||||
is_used: true // TODO
|
is_used: true // TODO
|
||||||
//is_alloc: p.is_alloc || typ.starts_with('array_')
|
// is_alloc: p.is_alloc || typ.starts_with('array_')
|
||||||
//line_nr: p.tokens[ var_token_idx ].line_nr
|
// line_nr: p.tokens[ var_token_idx ].line_nr
|
||||||
//token_idx: var_token_idx
|
// token_idx: var_token_idx
|
||||||
|
|
||||||
})
|
})
|
||||||
p.statements()
|
p.statements()
|
||||||
p.close_scope()
|
p.close_scope()
|
||||||
p.returns = false
|
p.returns = false
|
||||||
return 'void'
|
return 'void'
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.check_types(p.bool_expression(), 'bool')
|
p.check_types(p.bool_expression(), 'bool')
|
||||||
}
|
}
|
||||||
if is_expr {
|
if is_expr {
|
||||||
|
@ -297,7 +268,8 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
||||||
if p.tok == .key_else {
|
if p.tok == .key_else {
|
||||||
if p.inside_if_expr {
|
if p.inside_if_expr {
|
||||||
p.fspace()
|
p.fspace()
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.fgen_nl()
|
p.fgen_nl()
|
||||||
}
|
}
|
||||||
p.check(.key_else)
|
p.check(.key_else)
|
||||||
|
@ -348,5 +320,3 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
||||||
return typ
|
return typ
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -250,7 +250,9 @@ fn (p &Parser) gen_fmt() {
|
||||||
if s == '' {
|
if s == '' {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !p.file_path.contains('table.v') {return}
|
files := ['cgen.v', 'comptime.v', 'cc.v', 'if_match.v']
|
||||||
|
//if !p.file_path.contains('table.v') {return}
|
||||||
|
if !(p.file_name in files) { return }
|
||||||
path := os.tmpdir() + '/' + p.file_name
|
path := os.tmpdir() + '/' + p.file_name
|
||||||
println('generating ${path}')
|
println('generating ${path}')
|
||||||
mut out := os.create(path) or {
|
mut out := os.create(path) or {
|
||||||
|
|
Loading…
Reference in New Issue