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
is_arg bool
is_const bool
is_import_const bool // TODO remove import consts entirely
args []Var // function args
attr string // [json] etc
is_mut bool
@ -390,11 +389,11 @@ fn (p mut Parser) import_statement() {
}
fn (p mut Parser) const_decl() {
is_import := p.tok == .key_import
p.inside_const = true
if is_import {
p.next()
if p.tok == .key_import {
p.error('`import const` was removed from the language, ' +
'use `foo(C.CONST_NAME)` instead')
}
p.inside_const = true
p.check(.key_const)
p.fspace()
p.check(.lpar)
@ -406,20 +405,14 @@ fn (p mut Parser) const_decl() {
//if ! (name[0] >= `A` && name[0] <= `Z`) {
//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)
}
mut typ := 'int'
if !is_import {
p.check_space(.assign)
typ = p.expression()
}
if p.first_pass() && !is_import && p.table.known_const(name) {
typ := p.expression()
if p.first_pass() && p.table.known_const(name) {
p.error('redefinition of `$name`')
}
p.table.register_const(name, typ, p.mod, is_import)
if p.pass == .main && !is_import {
p.table.register_const(name, typ, p.mod)
if p.pass == .main {
// TODO hack
// 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
@ -707,7 +700,7 @@ fn (p mut Parser) enum_decl(_enum_name string) {
if p.tok == .comma {
p.next()
}
p.table.register_const(name, enum_name, p.mod, false)
p.table.register_const(name, enum_name, p.mod)
val++
}
p.table.register_type2(&Type {

View File

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

View File

@ -6,10 +6,6 @@ module builtin
#include <float.h>
import const (
DBL_EPSILON
)
pub fn (d double) str() string {
buf := malloc(sizeof(double) * 5 + 1)// TODO
C.sprintf(buf, '%f', d)
@ -36,7 +32,7 @@ pub fn ptr_str(ptr voidptr) string {
// compare floats using C epsilon
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 {

View File

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