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

remove from the language

This commit is contained in:
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) name = p.prepend_mod(name)
if !is_import { p.check_space(.assign)
name = p.prepend_mod(name) typ := p.expression()
} if p.first_pass() && p.table.known_const(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) {
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

@ -8,23 +8,23 @@ module os
#include <signal.h> #include <signal.h>
#include <errno.h> #include <errno.h>
//#include <execinfo.h> // for backtrace_symbols_fd //#include <execinfo.h> // for backtrace_symbols_fd
/* /*
struct dirent { struct dirent {
d_ino int d_ino int
d_off int d_off int
d_reclen u16 d_reclen u16
d_type byte d_type byte
d_name [256]byte d_name [256]byte
} }
*/ */
struct C.dirent { struct C.dirent {
d_name byteptr d_name byteptr
} }
fn C.readdir(voidptr) C.dirent fn C.readdir(voidptr) C.dirent
const ( const (
args = []string args = []string
@ -44,6 +44,7 @@ struct FileInfo {
size int size int
} }
/*
import const ( import const (
SEEK_SET SEEK_SET
SEEK_END SEEK_END
@ -57,11 +58,12 @@ import const (
SIGSEGV SIGSEGV
SIGTERM SIGTERM
) )
*/
struct C.stat { struct C.stat {
st_size int st_size int
st_mode int st_mode int
st_mtime int st_mtime int
} }
struct C.DIR { struct C.DIR {
@ -112,19 +114,19 @@ fn parse_windows_cmd_line(cmd byteptr) []string {
} }
// read_file reads the file in `path` and returns the contents. // read_file reads the file in `path` and returns the contents.
pub fn read_file(path string) ?string { pub fn read_file(path string) ?string {
mode := 'rb' mode := 'rb'
mut fp := &C.FILE{} mut fp := &C.FILE{}
$if windows { $if windows {
fp = C._wfopen(path.to_wide(), mode.to_wide()) fp = C._wfopen(path.to_wide(), mode.to_wide())
} $else { } $else {
cpath := path.str cpath := path.str
fp = C.fopen(cpath, mode.str) fp = C.fopen(cpath, mode.str)
} }
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)
@ -166,7 +168,7 @@ pub fn read_lines(path string) []string {
$if windows { $if windows {
fp = C._wfopen(path.to_wide(), mode.to_wide()) fp = C._wfopen(path.to_wide(), mode.to_wide())
} $else { } $else {
fp = C.fopen(path.str, mode.str) fp = C.fopen(path.str, mode.str)
} }
if isnil(fp) { if isnil(fp) {
// TODO // TODO
@ -219,15 +221,15 @@ pub fn open(path string) ?File {
cfile: C._wfopen(wpath, mode.to_wide()) cfile: C._wfopen(wpath, mode.to_wide())
} }
} $else { } $else {
cpath := path.str cpath := path.str
file = File { file = File {
cfile: C.fopen(cpath, 'rb') cfile: C.fopen(cpath, 'rb')
} }
} }
if isnil(file.cfile) { if isnil(file.cfile) {
return error('failed to open file "$path"') return error('failed to open file "$path"')
} }
return file return file
} }
// create creates a file at a specified location and returns a writable `File` object. // create creates a file at a specified location and returns a writable `File` object.
@ -240,15 +242,15 @@ pub fn create(path string) ?File {
cfile: C._wfopen(wpath, mode.to_wide()) cfile: C._wfopen(wpath, mode.to_wide())
} }
} $else { } $else {
cpath := path.str cpath := path.str
file = File { file = File {
cfile: C.fopen(cpath, 'wb') cfile: C.fopen(cpath, 'wb')
} }
} }
if isnil(file.cfile) { if isnil(file.cfile) {
return error('failed to create file "$path"') return error('failed to create file "$path"')
} }
return file return file
} }
pub fn open_append(path string) ?File { pub fn open_append(path string) ?File {
@ -260,15 +262,15 @@ pub fn open_append(path string) ?File {
cfile: C._wfopen(wpath, mode.to_wide()) cfile: C._wfopen(wpath, mode.to_wide())
} }
} $else { } $else {
cpath := path.str cpath := path.str
file = File { file = File {
cfile: C.fopen(cpath, 'ab') cfile: C.fopen(cpath, 'ab')
} }
} }
if isnil(file.cfile) { if isnil(file.cfile) {
return error('failed to create(append) file "$path"') return error('failed to create(append) file "$path"')
} }
return file return file
} }
pub fn (f File) write(s string) { pub fn (f File) write(s string) {
@ -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) {
@ -330,11 +332,11 @@ fn pclose(f *FILE) int {
} }
struct Result { struct Result {
pub: pub:
exit_code int exit_code int
output string output string
//stderr string // TODO //stderr string // TODO
} }
// exec starts the specified command, waits for it to complete, and returns its output. // exec starts the specified command, waits for it to complete, and returns its output.
pub fn exec(cmd string) ?Result { pub fn exec(cmd string) ?Result {
@ -343,10 +345,10 @@ pub fn exec(cmd string) ?Result {
if isnil(f) { if isnil(f) {
return error('exec("$cmd") failed') return error('exec("$cmd") failed')
} }
buf := [1000]byte buf := [1000]byte
mut res := '' mut res := ''
for C.fgets(buf, 1000, f) != 0 { for C.fgets(buf, 1000, f) != 0 {
res += tos(buf, strlen(buf)) res += tos(buf, strlen(buf))
} }
res = res.trim_space() res = res.trim_space()
exit_code := pclose(f)/256 exit_code := pclose(f)/256
@ -355,8 +357,8 @@ pub fn exec(cmd string) ?Result {
//} //}
return Result { return Result {
output: res output: res
exit_code: exit_code exit_code: exit_code
} }
} }
pub fn system(cmd string) int { pub fn system(cmd string) int {
@ -364,7 +366,7 @@ pub fn system(cmd string) int {
$if windows { $if windows {
ret = C._wsystem(cmd.to_wide()) ret = C._wsystem(cmd.to_wide())
} $else { } $else {
ret = C.system(cmd.str) ret = C.system(cmd.str)
} }
if ret == -1 { if ret == -1 {
os.print_c_errno() os.print_c_errno()
@ -398,10 +400,10 @@ pub fn setenv(name string, value string, overwrite bool) int {
} }
return -1 return -1
} }
$else { $else {
return C.setenv(name.str, value.str, overwrite) return C.setenv(name.str, value.str, overwrite)
} }
} }
pub fn unsetenv(name string) int { pub fn unsetenv(name string) int {
@ -409,10 +411,10 @@ pub fn unsetenv(name string) int {
format := '${name}=' format := '${name}='
return C._putenv(format.str) return C._putenv(format.str)
} }
$else { $else {
return C.unsetenv(name.str) return C.unsetenv(name.str)
} }
} }
// file_exists returns true if `path` exists. // file_exists returns true if `path` exists.
@ -449,7 +451,7 @@ pub fn rmdir(path string) {
fn print_c_errno() { fn print_c_errno() {
//C.printf('errno=%d err="%s"\n', errno, C.strerror(errno)) //C.printf('errno=%d err="%s"\n', errno, C.strerror(errno))
} }
@ -462,17 +464,17 @@ pub fn ext(path string) string {
} }
// dir returns all but the last element of path, typically the path's directory. // dir returns all but the last element of path, typically the path's directory.
pub fn dir(path string) string { pub fn dir(path string) string {
if path == '.' { if path == '.' {
return getwd() return getwd()
} }
pos := path.last_index(PathSeparator) pos := path.last_index(PathSeparator)
if pos == -1 { if pos == -1 {
return '.' return '.'
} }
return path.left(pos) return path.left(pos)
} }
fn path_sans_ext(path string) string { fn path_sans_ext(path string) string {
pos := path.last_index('.') pos := path.last_index('.')
@ -495,7 +497,7 @@ pub fn filename(path string) string {
return path.all_after('/') return path.all_after('/')
} }
// get_line returns a one-line string from stdin // get_line returns a one-line string from stdin
pub fn get_line() string { pub fn get_line() string {
str := get_raw_line() str := get_raw_line()
$if windows { $if windows {
@ -533,7 +535,7 @@ pub fn get_raw_line() string {
return '' return ''
} }
return string(buf, nr_chars) return string(buf, nr_chars)
} }
} }
pub fn get_lines() []string { pub fn get_lines() []string {
@ -575,16 +577,16 @@ pub fn user_os() string {
return 'windows' return 'windows'
} }
$if freebsd { $if freebsd {
return 'freebsd' return 'freebsd'
} }
$if openbsd { $if openbsd {
return 'openbsd' return 'openbsd'
} }
$if netbsd { $if netbsd {
return 'netbsd' return 'netbsd'
} }
$if dragonfly { $if dragonfly {
return 'dragonfly' return 'dragonfly'
} }
$if msvc { $if msvc {
return 'windows' return 'windows'
@ -610,11 +612,11 @@ pub fn home_dir() string {
return home return home
} }
// write_file writes `text` data to a file in `path`. // write_file writes `text` data to a file in `path`.
pub fn write_file(path, text string) { pub fn write_file(path, text string) {
f := os.create(path) or { f := os.create(path) or {
return return
} }
f.write(text) f.write(text)
f.close() f.close()
} }
@ -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)
} }
} }
@ -655,26 +657,26 @@ pub fn executable() string {
} }
$if mac { $if mac {
mut result := malloc(MAX_PATH) mut result := malloc(MAX_PATH)
pid := C.getpid() pid := C.getpid()
ret := C.proc_pidpath (pid, result, MAX_PATH) ret := C.proc_pidpath (pid, result, MAX_PATH)
if ret <= 0 { if ret <= 0 {
println('os.executable() failed') println('os.executable() failed')
return '.' return '.'
} }
return string(result) return string(result)
} }
$if freebsd { $if freebsd {
mut result := malloc(MAX_PATH) mut result := malloc(MAX_PATH)
mib := [1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1] mib := [1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1]
size := MAX_PATH size := MAX_PATH
C.sysctl(mib.data, 4, result, &size, 0, 0) C.sysctl(mib.data, 4, result, &size, 0, 0)
return string(result) return string(result)
} }
$if openbsd { $if openbsd {
// "Sadly there is no way to get the full path of the executed file in OpenBSD." // "Sadly there is no way to get the full path of the executed file in OpenBSD."
// lol // lol
return os.args[0] return os.args[0]
} }
$if netbsd { $if netbsd {
mut result := malloc(MAX_PATH) mut result := malloc(MAX_PATH)
count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH )) count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH ))
@ -682,7 +684,7 @@ pub fn executable() string {
panic('error reading /proc/curproc/exe to get exe path') panic('error reading /proc/curproc/exe to get exe path')
} }
return string(result, count) return string(result, count)
} }
$if dragonfly { $if dragonfly {
mut result := malloc(MAX_PATH) mut result := malloc(MAX_PATH)
count := int(C.readlink('/proc/curproc/file', result, MAX_PATH )) count := int(C.readlink('/proc/curproc/file', result, MAX_PATH ))
@ -690,18 +692,18 @@ pub fn executable() string {
panic('error reading /proc/curproc/file to get exe path') panic('error reading /proc/curproc/file to get exe path')
} }
return string(result, count) return string(result, count)
} }
return '.' return '.'
} }
pub fn is_dir(path string) bool { pub fn is_dir(path string) bool {
$if windows { $if windows {
return dir_exists(path) return dir_exists(path)
//val := int(C.GetFileAttributes(path.to_wide())) //val := int(C.GetFileAttributes(path.to_wide()))
// Note: this return is broke (wrong). we have dir_exists already how will this differ? // Note: this return is broke (wrong). we have dir_exists already how will this differ?
//return (val &FILE_ATTRIBUTE_DIRECTORY) > 0 //return (val &FILE_ATTRIBUTE_DIRECTORY) > 0
} }
$else { $else {
statbuf := C.stat{} statbuf := C.stat{}
cstr := path.str cstr := path.str
if C.stat(cstr, &statbuf) != 0 { if C.stat(cstr, &statbuf) != 0 {
@ -709,16 +711,16 @@ pub fn is_dir(path string) bool {
} }
// ref: https://code.woboq.org/gcc/include/sys/stat.h.html // ref: https://code.woboq.org/gcc/include/sys/stat.h.html
return (statbuf.st_mode & S_IFMT) == S_IFDIR return (statbuf.st_mode & S_IFMT) == S_IFDIR
} }
} }
pub fn chdir(path string) { pub fn chdir(path string) {
$if windows { $if windows {
C._wchdir(path.to_wide()) C._wchdir(path.to_wide())
} }
$else { $else {
C.chdir(path.str) C.chdir(path.str)
} }
} }
pub fn getwd() string { pub fn getwd() string {
@ -731,7 +733,7 @@ pub fn getwd() string {
return string_from_wide(buf) return string_from_wide(buf)
} }
$else { $else {
buf := malloc(512) buf := malloc(512)
if C.getcwd(buf, 512) == 0 { if C.getcwd(buf, 512) == 0 {
return '' return ''
} }
@ -759,27 +761,27 @@ pub fn realpath(fpath string) string {
return fpath return fpath
} }
// walk_ext returns a recursive list of all file paths ending with `ext`. // walk_ext returns a recursive list of all file paths ending with `ext`.
pub fn walk_ext(path, ext string) []string { pub fn walk_ext(path, ext string) []string {
if !os.is_dir(path) { if !os.is_dir(path) {
return []string return []string
} }
mut files := os.ls(path) mut files := os.ls(path)
mut res := []string mut res := []string
for i, file in files { for i, file in files {
if file.starts_with('.') { if file.starts_with('.') {
continue continue
} }
p := path + '/' + file p := path + '/' + file
if os.is_dir(p) { if os.is_dir(p) {
res << walk_ext(p, ext) res << walk_ext(p, ext)
} }
else if file.ends_with(ext) { else if file.ends_with(ext) {
res << p res << p
} }
} }
return res return res
} }
pub fn signal(signum int, handler voidptr) { pub fn signal(signum int, handler voidptr) {
C.signal(signum, handler) C.signal(signum, handler)
@ -802,28 +804,28 @@ pub fn wait() int {
} }
pub fn file_last_mod_unix(path string) int { pub fn file_last_mod_unix(path string) int {
attr := C.stat{} attr := C.stat{}
//# struct stat attr; //# struct stat attr;
C.stat(path.str, &attr) C.stat(path.str, &attr)
//# stat(path.str, &attr); //# stat(path.str, &attr);
return attr.st_mtime return attr.st_mtime
//# return attr.st_mtime ; //# return attr.st_mtime ;
} }
fn log(s string) { fn log(s string) {
} }
pub fn flush_stdout() { pub fn flush_stdout() {
C.fflush(stdout) C.fflush(stdout)
} }
pub fn print_backtrace() { pub fn print_backtrace() {
/* /*
# void *buffer[100]; # void *buffer[100];
nptrs := 0 nptrs := 0
# nptrs = backtrace(buffer, 100); # nptrs = backtrace(buffer, 100);
# printf("%d!!\n", nptrs); # printf("%d!!\n", nptrs);
# backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ; # backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ;
*/ */
} }