remove from the language

pull/1714/head
Alexander Medvednikov 2019-08-22 23:19:31 +03:00
parent 2eb9440095
commit dcfc9eb1a1
4 changed files with 135 additions and 145 deletions

View File

@ -16,7 +16,6 @@ mut:
name string name string
is_arg bool is_arg bool
is_const bool is_const bool
is_import_const bool // TODO remove import consts entirely
args []Var // function args args []Var // function args
attr string // [json] etc attr string // [json] etc
is_mut bool is_mut bool
@ -390,11 +389,11 @@ fn (p mut Parser) import_statement() {
} }
fn (p mut Parser) const_decl() { fn (p mut Parser) const_decl() {
is_import := p.tok == .key_import if p.tok == .key_import {
p.inside_const = true p.error('`import const` was removed from the language, ' +
if is_import { 'use `foo(C.CONST_NAME)` instead')
p.next()
} }
p.inside_const = true
p.check(.key_const) p.check(.key_const)
p.fspace() p.fspace()
p.check(.lpar) p.check(.lpar)
@ -406,20 +405,14 @@ fn (p mut Parser) const_decl() {
//if ! (name[0] >= `A` && name[0] <= `Z`) { //if ! (name[0] >= `A` && name[0] <= `Z`) {
//p.error('const name must be capitalized') //p.error('const name must be capitalized')
//} //}
// Imported consts (like GL_TRIANG.leS) dont need mod prepended (gl__GL_TRIANG.leS)
if !is_import {
name = p.prepend_mod(name) name = p.prepend_mod(name)
}
mut typ := 'int'
if !is_import {
p.check_space(.assign) p.check_space(.assign)
typ = p.expression() typ := p.expression()
} if p.first_pass() && p.table.known_const(name) {
if p.first_pass() && !is_import && p.table.known_const(name) {
p.error('redefinition of `$name`') p.error('redefinition of `$name`')
} }
p.table.register_const(name, typ, p.mod, is_import) p.table.register_const(name, typ, p.mod)
if p.pass == .main && !is_import { if p.pass == .main {
// TODO hack // TODO hack
// cur_line has const's value right now. if it's just a number, then optimize generation: // cur_line has const's value right now. if it's just a number, then optimize generation:
// output a #define so that we don't pollute the binary with unnecessary global vars // output a #define so that we don't pollute the binary with unnecessary global vars
@ -707,7 +700,7 @@ fn (p mut Parser) enum_decl(_enum_name string) {
if p.tok == .comma { if p.tok == .comma {
p.next() p.next()
} }
p.table.register_const(name, enum_name, p.mod, false) p.table.register_const(name, enum_name, p.mod)
val++ val++
} }
p.table.register_type2(&Type { p.table.register_type2(&Type {

View File

@ -174,10 +174,10 @@ fn new_table(obfuscate bool) *Table {
t.register_type('voidptr') t.register_type('voidptr')
t.register_type('T') t.register_type('T')
t.register_type('va_list') t.register_type('va_list')
t.register_const('stdin', 'int', 'main', false) t.register_const('stdin', 'int', 'main')
t.register_const('stdout', 'int', 'main', false) t.register_const('stdout', 'int', 'main')
t.register_const('stderr', 'int', 'main', false) t.register_const('stderr', 'int', 'main')
t.register_const('errno', 'int', 'main', false) t.register_const('errno', 'int', 'main')
t.register_type_with_parent('map_string', 'map') t.register_type_with_parent('map_string', 'map')
t.register_type_with_parent('map_int', 'map') t.register_type_with_parent('map_int', 'map')
return t return t
@ -226,12 +226,11 @@ fn (table &Table) known_mod(mod string) bool {
return mod in table.modules return mod in table.modules
} }
fn (t mut Table) register_const(name, typ, mod string, is_imported bool) { fn (t mut Table) register_const(name, typ, mod string) {
t.consts << Var { t.consts << Var {
name: name name: name
typ: typ typ: typ
is_const: true is_const: true
is_import_const: is_imported
mod: mod mod: mod
} }
} }

View File

@ -6,10 +6,6 @@ module builtin
#include <float.h> #include <float.h>
import const (
DBL_EPSILON
)
pub fn (d double) str() string { pub fn (d double) str() string {
buf := malloc(sizeof(double) * 5 + 1)// TODO buf := malloc(sizeof(double) * 5 + 1)// TODO
C.sprintf(buf, '%f', d) C.sprintf(buf, '%f', d)
@ -36,7 +32,7 @@ pub fn ptr_str(ptr voidptr) string {
// compare floats using C epsilon // compare floats using C epsilon
pub fn (a f64) eq(b f64) bool { pub fn (a f64) eq(b f64) bool {
return C.fabs(a - b) <= DBL_EPSILON return C.fabs(a - b) <= C.DBL_EPSILON
} }
// fn (nn i32) str() string { // fn (nn i32) str() string {

View File

@ -44,6 +44,7 @@ struct FileInfo {
size int size int
} }
/*
import const ( import const (
SEEK_SET SEEK_SET
SEEK_END SEEK_END
@ -57,6 +58,7 @@ import const (
SIGSEGV SIGSEGV
SIGTERM SIGTERM
) )
*/
struct C.stat { struct C.stat {
st_size int st_size int
@ -124,7 +126,7 @@ pub fn read_file(path string) ?string {
if isnil(fp) { if isnil(fp) {
return error('failed to open file "$path"') return error('failed to open file "$path"')
} }
C.fseek(fp, 0, SEEK_END) C.fseek(fp, 0, C.SEEK_END)
fsize := C.ftell(fp) fsize := C.ftell(fp)
// C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below // C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below
C.rewind(fp) C.rewind(fp)
@ -284,9 +286,9 @@ pub fn (f File) write_bytes(data voidptr, size int) {
} }
pub fn (f File) write_bytes_at(data voidptr, size, pos int) { pub fn (f File) write_bytes_at(data voidptr, size, pos int) {
C.fseek(f.cfile, pos, SEEK_SET) C.fseek(f.cfile, pos, C.SEEK_SET)
C.fwrite(data, 1, size, f.cfile) C.fwrite(data, 1, size, f.cfile)
C.fseek(f.cfile, 0, SEEK_END) C.fseek(f.cfile, 0, C.SEEK_END)
} }
pub fn (f File) writeln(s string) { pub fn (f File) writeln(s string) {
@ -633,8 +635,8 @@ fn on_segfault(f voidptr) {
C.memset(&sa, 0, sizeof(sigaction)) C.memset(&sa, 0, sizeof(sigaction))
C.sigemptyset(&sa.sa_mask) C.sigemptyset(&sa.sa_mask)
sa.sa_sigaction = f sa.sa_sigaction = f
sa.sa_flags = SA_SIGINFO sa.sa_flags = C.SA_SIGINFO
C.sigaction(SIGSEGV, &sa, 0) C.sigaction(C.SIGSEGV, &sa, 0)
} }
} }