1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v2: enable compilation of short programs with no 'import os'

This commit is contained in:
Delyan Angelov 2020-03-26 10:20:55 +02:00 committed by GitHub
parent d8bcd13e86
commit fb4f7b6827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 11 deletions

View File

@ -665,10 +665,12 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
}
if is_main {
g.writeln('_vinit();')
if g.autofree {
g.writeln('free(_const_os__args.data); // empty, inited in _vinit()')
if g.is_importing_os() {
if g.autofree {
g.writeln('free(_const_os__args.data); // empty, inited in _vinit()')
}
g.writeln('_const_os__args = os__init_os_args(argc, (byteptr*)argv);')
}
g.writeln('_const_os__args = os__init_os_args(argc, (byteptr*)argv);')
}
g.stmts(it.stmts)
// ////////////
@ -1794,7 +1796,10 @@ fn (g mut Gen) write_init_function() {
if g.autofree {
g.writeln('void _vcleanup() {')
g.writeln('puts("cleaning up...");')
g.writeln('free(_const_os__args.data);')
if g.is_importing_os() {
g.writeln('free(_const_os__args.data);')
g.writeln('string_free(_const_os__wd_at_startup);')
}
g.writeln('free(_const_strconv__ftoa__powers_of_10.data);')
g.writeln('}')
}
@ -2318,3 +2323,7 @@ pub fn (g mut Gen) write_tests_main() {
}
g.writeln('return 0; }')
}
fn (g &Gen) is_importing_os() bool {
return 'os' in g.table.imports
}

View File

@ -62,9 +62,9 @@ fn compare_texts(a, b, path string) bool {
line_b := lines_b[i]
if line_a.trim_space() != line_b.trim_space() {
println('${path}: Got\n$a')
println('${term_fail} near line: ${i}')
println(term.red('actual :${line_a}'))
println(term.red('expected:${line_b}'))
println('${path}:${i}: ${term_fail}')
println(term.bold(term.bright_yellow('actual : ')) + line_a)
println(term.green('expected: ') + line_b)
println(lines_b[i + 1])
println(lines_b[i + 2])
// exit(1)

View File

@ -48,7 +48,6 @@ struct varg_int {
//
int main(int argc, char** argv) {
_vinit();
_const_os__args = os__init_os_args(argc, (byteptr*)argv);
int a = 10;
a++;
int negative = -a;

View File

@ -78,7 +78,6 @@ void end() {
int main(int argc, char** argv) {
_vinit();
_const_os__args = os__init_os_args(argc, (byteptr*)argv);
return 0;
}

View File

@ -64,7 +64,6 @@ void handle_expr(Expr e) {
int main(int argc, char** argv) {
_vinit();
_const_os__args = os__init_os_args(argc, (byteptr*)argv);
User user = (User){
.age = 0,
.name = tos3(""),

View File

@ -24,7 +24,6 @@ int Bar_testa(Bar* b);
int main(int argc, char** argv) {
_vinit();
_const_os__args = os__init_os_args(argc, (byteptr*)argv);
Bar b = (Bar){
.a = 122,
};