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

snake_case consts

This commit is contained in:
Alexander Medvednikov 2019-10-12 22:31:05 +03:00
parent 81f8b26127
commit a76165828b
17 changed files with 124 additions and 125 deletions

View File

@ -65,11 +65,11 @@ 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.PathSeparator}cache${os.PathSeparator}$v.dir' '$v_modules_path${os.path_separator}cache${os.path_separator}$v.dir'
} else { } else {
'$v_modules_path${os.PathSeparator}$v.dir' '$v_modules_path${os.path_separator}$v.dir'
} }
pdir := out_dir.all_before_last(os.PathSeparator) pdir := out_dir.all_before_last(os.path_separator)
if !os.dir_exists(pdir) { if !os.dir_exists(pdir) {
os.mkdir_all(pdir) os.mkdir_all(pdir)
} }
@ -118,25 +118,25 @@ fn (v mut V) cc() {
} }
else if v.pref.is_cache { else if v.pref.is_cache {
vexe := os.executable() vexe := os.executable()
builtin_o_path := '$v_modules_path${os.PathSeparator}cache${os.PathSeparator}vlib${os.PathSeparator}builtin.o' builtin_o_path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}builtin.o'
if os.file_exists(builtin_o_path) { if os.file_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.PathSeparator}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') { continue } // not working
if imp == 'webview' { continue } if imp == 'webview' { continue }
imp_path := imp.replace('.', os.PathSeparator) imp_path := imp.replace('.', os.path_separator)
path := '$v_modules_path${os.PathSeparator}cache${os.PathSeparator}vlib${os.PathSeparator}${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.file_exists(path) { if os.file_exists(path) {
libs += ' ' + path libs += ' ' + path
} else { } else {
println('$path not found... building module $imp') println('$path not found... building module $imp')
os.system('$vexe build module vlib${os.PathSeparator}$imp_path') os.system('$vexe build module vlib${os.path_separator}$imp_path')
} }
} }
} }

View File

@ -254,7 +254,7 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
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.PathSeparator + file ) + '" ' cfiles += '"' + os.realpath( parent + os.path_separator + file ) + '" '
} }
} }
cc := find_c_compiler() cc := find_c_compiler()

View File

@ -610,7 +610,7 @@ fn (v &V) v_files_from_dir(dir string) []string {
if file.ends_with('_c.v') && v.os == .js { if file.ends_with('_c.v') && v.os == .js {
continue continue
} }
res << '$dir${os.PathSeparator}$file' res << '$dir${os.path_separator}$file'
} }
return res return res
} }
@ -619,7 +619,7 @@ fn (v &V) v_files_from_dir(dir string) []string {
fn (v mut V) add_v_files_to_compile() { fn (v mut V) add_v_files_to_compile() {
mut builtin_files := v.get_builtin_files() mut builtin_files := v.get_builtin_files()
// Builtin cache exists? Use it. // Builtin cache exists? Use it.
builtin_vh := '$v_modules_path${os.PathSeparator}vlib${os.PathSeparator}builtin.vh' builtin_vh := '$v_modules_path${os.path_separator}vlib${os.path_separator}builtin.vh'
if v.pref.is_cache && os.file_exists(builtin_vh) { if v.pref.is_cache && os.file_exists(builtin_vh) {
v.cached_mods << 'builtin' v.cached_mods << 'builtin'
builtin_files = [builtin_vh] builtin_files = [builtin_vh]
@ -638,7 +638,7 @@ fn (v mut V) add_v_files_to_compile() {
mut p := v.new_parser_from_file(file) mut p := v.new_parser_from_file(file)
// set mod so we dont have to resolve submodule // set mod so we dont have to resolve submodule
if v.pref.build_mode == .build_module && if v.pref.build_mode == .build_module &&
file.contains(v.mod.replace('.', os.PathSeparator)) { file.contains(v.mod.replace('.', os.path_separator)) {
p.mod = v.mod p.mod = v.mod
} }
p.parse(.imports) p.parse(.imports)
@ -662,8 +662,8 @@ fn (v mut V) add_v_files_to_compile() {
// use cached built module if exists // use cached built module if exists
if v.pref.build_mode != .build_module && !mod.contains('vweb') { if v.pref.build_mode != .build_module && !mod.contains('vweb') {
mod_path := mod.replace('.', os.PathSeparator) mod_path := mod.replace('.', os.path_separator)
vh_path := '$v_modules_path${os.PathSeparator}vlib${os.PathSeparator}${mod_path}.vh' vh_path := '$v_modules_path${os.path_separator}vlib${os.path_separator}${mod_path}.vh'
if v.pref.is_cache && os.file_exists(vh_path) { if v.pref.is_cache && os.file_exists(vh_path) {
println('using cached module `$mod`: $vh_path') println('using cached module `$mod`: $vh_path')
v.cached_mods << mod v.cached_mods << mod
@ -689,9 +689,9 @@ fn (v &V) get_builtin_files() []string {
// .vh cache exists? Use it // .vh cache exists? Use it
$if js { $if js {
return v.v_files_from_dir('$v.vroot${os.PathSeparator}vlib${os.PathSeparator}builtin${os.PathSeparator}js') return v.v_files_from_dir('$v.vroot${os.path_separator}vlib${os.path_separator}builtin${os.path_separator}js')
} }
return v.v_files_from_dir('$v.vroot${os.PathSeparator}vlib${os.PathSeparator}builtin') return v.v_files_from_dir('$v.vroot${os.path_separator}vlib${os.path_separator}builtin')
} }
// get user files // get user files
@ -703,22 +703,22 @@ fn (v &V) get_user_files() []string {
mut user_files := []string mut user_files := []string
if v.pref.is_test && v.pref.is_stats { if v.pref.is_test && v.pref.is_stats {
user_files << [v.vroot, 'vlib', 'benchmark', 'tests', 'always_imported.v'].join( os.PathSeparator ) user_files << [v.vroot, 'vlib', 'benchmark', 'tests', 'always_imported.v'].join( os.path_separator )
} }
// v volt/slack_test.v: compile all .v files to get the environment // v volt/slack_test.v: compile all .v files to get the environment
// I need to implement user packages! TODO // I need to implement user packages! TODO
is_test_with_imports := dir.ends_with('_test.v') && is_test_with_imports := dir.ends_with('_test.v') &&
(dir.contains('${os.PathSeparator}volt') || dir.contains('${os.PathSeparator}c2volt'))// TODO (dir.contains('${os.path_separator}volt') || dir.contains('${os.path_separator}c2volt'))// TODO
if is_test_with_imports { if is_test_with_imports {
user_files << dir user_files << dir
pos := dir.last_index(os.PathSeparator) pos := dir.last_index(os.path_separator)
dir = dir.left(pos) + os.PathSeparator// TODO WHY IS THIS .neEDED? dir = dir.left(pos) + os.path_separator// TODO WHY IS THIS .neEDED?
} }
if dir.ends_with('.v') { if dir.ends_with('.v') {
// Just compile one file and get parent dir // Just compile one file and get parent dir
user_files << dir user_files << dir
dir = dir.all_before('${os.PathSeparator}') dir = dir.all_before('${os.path_separator}')
} }
else { else {
// Add .v files from the directory being compiled // Add .v files from the directory being compiled
@ -815,7 +815,7 @@ fn new_v(args[]string) &V {
// Create modules dirs if they are missing // Create modules dirs if they are missing
if !os.dir_exists(v_modules_path) { if !os.dir_exists(v_modules_path) {
os.mkdir(v_modules_path) os.mkdir(v_modules_path)
os.mkdir('$v_modules_path${os.PathSeparator}cache') os.mkdir('$v_modules_path${os.path_separator}cache')
} }
mut vgen_buf := strings.new_builder(1000) mut vgen_buf := strings.new_builder(1000)
@ -829,10 +829,10 @@ fn new_v(args[]string) &V {
if 'run' in args { if 'run' in args {
dir = get_param_after(joined_args, 'run', '') dir = get_param_after(joined_args, 'run', '')
} }
if dir.ends_with(os.PathSeparator) { if dir.ends_with(os.path_separator) {
dir = dir.all_before_last(os.PathSeparator) dir = dir.all_before_last(os.path_separator)
} }
if dir.starts_with('.$os.PathSeparator') { if dir.starts_with('.$os.path_separator') {
dir = dir.right(2) dir = dir.right(2)
} }
if args.len < 2 { if args.len < 2 {
@ -845,17 +845,17 @@ fn new_v(args[]string) &V {
build_mode = .build_module build_mode = .build_module
// v build module ~/v/os => os.o // v build module ~/v/os => os.o
mod_path := if dir.contains('vlib') { mod_path := if dir.contains('vlib') {
dir.all_after('vlib'+os.PathSeparator) dir.all_after('vlib'+os.path_separator)
} }
else if dir.starts_with('.\\') || dir.starts_with('./') { else if dir.starts_with('.\\') || dir.starts_with('./') {
dir.right(2) dir.right(2)
} }
else if dir.starts_with(os.PathSeparator) { else if dir.starts_with(os.path_separator) {
dir.all_after(os.PathSeparator) dir.all_after(os.path_separator)
} else { } else {
dir dir
} }
mod = mod_path.replace(os.PathSeparator, '.') mod = mod_path.replace(os.path_separator, '.')
println('Building module "${mod}" (dir="$dir")...') println('Building module "${mod}" (dir="$dir")...')
//out_name = '$TmpPath/vlib/${base}.o' //out_name = '$TmpPath/vlib/${base}.o'
out_name = mod out_name = mod
@ -881,7 +881,7 @@ fn new_v(args[]string) &V {
} }
// if we are in `/foo` and run `v .`, the executable should be `foo` // if we are in `/foo` and run `v .`, the executable should be `foo`
if dir == '.' && out_name == 'a.out' { if dir == '.' && out_name == 'a.out' {
base := os.getwd().all_after(os.PathSeparator) base := os.getwd().all_after(os.path_separator)
out_name = base.trim_space() out_name = base.trim_space()
} }
mut _os := OS.mac mut _os := OS.mac
@ -976,7 +976,7 @@ fn new_v(args[]string) &V {
println('C compiler=$pref.ccompiler') println('C compiler=$pref.ccompiler')
} }
if pref.is_so { if pref.is_so {
out_name_c = out_name.all_after(os.PathSeparator) + '_shared_lib.c' out_name_c = out_name.all_after(os.path_separator) + '_shared_lib.c'
} }
return &V{ return &V{
os: _os os: _os

View File

@ -106,14 +106,14 @@ fn v_type_str(typ_ string) string {
fn (v &V) generate_vh() { fn (v &V) generate_vh() {
println('\n\n\n\nGenerating a V header file for module `$v.mod`') println('\n\n\n\nGenerating a V header file for module `$v.mod`')
mod_path := v.mod.replace('.', os.PathSeparator) mod_path := v.mod.replace('.', os.path_separator)
dir := if v.dir.starts_with('vlib') { dir := if v.dir.starts_with('vlib') {
'$v_modules_path${os.PathSeparator}$v.dir' '$v_modules_path${os.path_separator}$v.dir'
} else { } else {
'$v_modules_path${os.PathSeparator}$mod_path' '$v_modules_path${os.path_separator}$mod_path'
} }
path := dir + '.vh' path := dir + '.vh'
pdir := dir.all_before_last(os.PathSeparator) pdir := dir.all_before_last(os.path_separator)
if !os.dir_exists(pdir) { if !os.dir_exists(pdir) {
os.mkdir_all(pdir) os.mkdir_all(pdir)
// os.mkdir(os.realpath(dir)) // os.mkdir(os.realpath(dir))

View File

@ -33,7 +33,7 @@ pub fn(graph &DepGraph) imports() []string {
fn (v &V) module_path(mod string) string { fn (v &V) module_path(mod string) string {
// submodule support // submodule support
if mod.contains('.') { if mod.contains('.') {
return mod.replace('.', os.PathSeparator) return mod.replace('.', os.path_separator)
// return mod.replace('.', '/') // return mod.replace('.', '/')
} }
return mod return mod
@ -45,15 +45,15 @@ fn (v &V) module_path(mod string) string {
fn (v &V) find_module_path(mod string) ?string { fn (v &V) find_module_path(mod string) ?string {
mod_path := v.module_path(mod) mod_path := v.module_path(mod)
// First check for local modules in the same directory // First check for local modules in the same directory
mut import_path := os.getwd() + '${os.PathSeparator}$mod_path' mut import_path := os.getwd() + '${os.path_separator}$mod_path'
// Now search in vlib/ // Now search in vlib/
if !os.dir_exists(import_path) { if !os.dir_exists(import_path) {
import_path = '$v.lang_dir${os.PathSeparator}vlib${os.PathSeparator}$mod_path' import_path = '$v.lang_dir${os.path_separator}vlib${os.path_separator}$mod_path'
} }
//println('ip=$import_path') //println('ip=$import_path')
// Finally try modules installed with vpm (~/.vmodules) // Finally try modules installed with vpm (~/.vmodules)
if !os.dir_exists(import_path) { if !os.dir_exists(import_path) {
import_path = '$v_modules_path${os.PathSeparator}$mod_path' import_path = '$v_modules_path${os.path_separator}$mod_path'
if !os.dir_exists(import_path){ if !os.dir_exists(import_path){
return error('module "$mod" not found') return error('module "$mod" not found')
} }

View File

@ -191,7 +191,7 @@ fn find_msvc() ?MsvcResult {
} }
return MsvcResult { return MsvcResult {
full_cl_exe_path: os.realpath( vs.exe_path + os.PathSeparator + 'cl.exe' ) full_cl_exe_path: os.realpath( vs.exe_path + os.path_separator + 'cl.exe' )
exe_path: vs.exe_path, exe_path: vs.exe_path,
um_lib_path: wk.um_lib_path, um_lib_path: wk.um_lib_path,
@ -388,7 +388,7 @@ fn build_thirdparty_obj_file_with_msvc(path string, moduleflags []CFlag) {
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.PathSeparator + file ) + '" ' cfiles += '"' + os.realpath( parent + os.path_separator + file ) + '" '
} }
} }
@ -441,7 +441,7 @@ fn (cflags []CFlag) msvc_string_flags() MsvcStringFlags {
} }
else if flag.name == '-L' { else if flag.name == '-L' {
lib_paths << flag.value lib_paths << flag.value
lib_paths << flag.value + os.PathSeparator + 'msvc' lib_paths << flag.value + os.path_separator + 'msvc'
// The above allows putting msvc specific .lib files in a subfolder msvc/ , // The above allows putting msvc specific .lib files in a subfolder msvc/ ,
// where gcc will NOT find them, but cl will do... // where gcc will NOT find them, but cl will do...
// NB: gcc is smart enough to not need .lib files at all in most cases, the .dll is enough. // NB: gcc is smart enough to not need .lib files at all in most cases, the .dll is enough.

View File

@ -122,7 +122,7 @@ fn (v mut V) new_parser_from_file(path string) Parser {
mut p := v.new_parser(new_scanner_file(path), path) mut p := v.new_parser(new_scanner_file(path), path)
p = { p| p = { p|
file_path: path, file_path: path,
file_name: path.all_after(os.PathSeparator), file_name: path.all_after(os.path_separator),
file_platform: path_platform, file_platform: path_platform,
file_pcguard: path_pcguard, file_pcguard: path_pcguard,
is_script: (v.pref.is_script && os.realpath(path) == os.realpath(path)), is_script: (v.pref.is_script && os.realpath(path) == os.realpath(path)),
@ -518,8 +518,8 @@ fn (p mut Parser) const_decl() {
continue continue
} }
mut name := p.check_name() // `Age = 20` mut name := p.check_name() // `Age = 20`
if p.mod != 'os' && contains_capital(name) { if !p.pref.building_v && p.mod != 'os' && contains_capital(name) {
//p.warn('const names cannot contain uppercase letters, use snake_case instead') p.warn('const names cannot contain uppercase letters, use snake_case instead')
} }
name = p.prepend_mod(name) name = p.prepend_mod(name)
mut typ := '' mut typ := ''

View File

@ -725,7 +725,7 @@ fn (s mut Scanner) debug_tokens() {
s.started = false s.started = false
s.debug = true s.debug = true
fname := s.file_path.all_after(os.PathSeparator) fname := s.file_path.all_after(os.path_separator)
println('\n===DEBUG TOKENS $fname===') println('\n===DEBUG TOKENS $fname===')
for { for {

View File

@ -778,14 +778,14 @@ fn (table &Table) cgen_name_type_pair(name, typ string) string {
fn is_valid_int_const(val, typ string) bool { fn is_valid_int_const(val, typ string) bool {
x := val.int() x := val.int()
switch typ { switch typ {
case 'byte': return 0 <= x && x <= math.MaxU8 case 'byte': return 0 <= x && x <= math.max_u8
case 'u16': return 0 <= x && x <= math.MaxU16 case 'u16': return 0 <= x && x <= math.max_u16
//case 'u32': return 0 <= x && x <= math.MaxU32 //case 'u32': return 0 <= x && x <= math.MaxU32
//case 'u64': return 0 <= x && x <= math.MaxU64 //case 'u64': return 0 <= x && x <= math.MaxU64
////////////// //////////////
case 'i8': return math.MinI8 <= x && x <= math.MaxI8 case 'i8': return math.min_i8 <= x && x <= math.max_i8
case 'i16': return math.MinI16 <= x && x <= math.MaxI16 case 'i16': return math.min_i16 <= x && x <= math.max_i16
case 'int': return math.MinI32 <= x && x <= math.MaxI32 case 'int': return math.min_i32 <= x && x <= math.max_i32
//case 'i64': //case 'i64':
//x64 := val.i64() //x64 := val.i64()
//return i64(-(1<<63)) <= x64 && x64 <= i64((1<<63)-1) //return i64(-(1<<63)) <= x64 && x64 <= i64((1<<63)-1)
@ -870,7 +870,7 @@ fn (table &Table) qualify_module(mod string, file_path string) string {
for m in table.imports { for m in table.imports {
if m.contains('.') && m.contains(mod) { if m.contains('.') && m.contains(mod) {
m_parts := m.split('.') m_parts := m.split('.')
m_path := m_parts.join(os.PathSeparator) m_path := m_parts.join(os.path_separator)
if mod == m_parts[m_parts.len-1] && file_path.contains(m_path) { if mod == m_parts[m_parts.len-1] && file_path.contains(m_path) {
return m return m
} }

View File

@ -11,12 +11,12 @@ pub:
pub fn full_path_to_v() string { pub fn full_path_to_v() string {
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' } vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
vexec := os.dir(os.dir(os.dir(os.dir( os.executable() )))) + os.PathSeparator + vname vexec := os.dir(os.dir(os.dir(os.dir( os.executable() )))) + os.path_separator + vname
/* /*
args := os.args args := os.args
vreal := os.realpath('v') vreal := os.realpath('v')
myself := os.realpath( os.executable() ) myself := os.realpath( os.executable() )
wd := os.getwd() + os.PathSeparator wd := os.getwd() + os.path_separator
println('args are: $args') println('args are: $args')
println('vreal : $vreal') println('vreal : $vreal')
println('myself : $myself') println('myself : $myself')
@ -76,7 +76,7 @@ $diff
} }
pub fn new_options() RunnerOptions { pub fn new_options() RunnerOptions {
wd := os.getwd() + os.PathSeparator wd := os.getwd() + os.path_separator
vexec := full_path_to_v() vexec := full_path_to_v()
mut files := []string mut files := []string
if os.args.len > 1 { if os.args.len > 1 {

View File

@ -56,7 +56,7 @@ fn test_v() {
} }
if os.dir_exists(targ) { if os.dir_exists(targ) {
ts.files << os.walk_ext( targ.trim_right(os.PathSeparator), '_test.v') ts.files << os.walk_ext( targ.trim_right(os.path_separator), '_test.v')
continue continue
} }
println('Unrecognized test file $targ .') println('Unrecognized test file $targ .')

View File

@ -172,7 +172,7 @@ fn (c Context) compare_v_performance( commands []string ) {
for cmd in commands { hyperfine_commands_arguments << ' \'cd ${c.b:30s} ; ./$cmd \' ' } for cmd in commands { hyperfine_commands_arguments << ' \'cd ${c.b:30s} ; ./$cmd \' ' }
for cmd in commands { hyperfine_commands_arguments << ' \'cd ${c.a:30s} ; ./$cmd \' ' } for cmd in commands { hyperfine_commands_arguments << ' \'cd ${c.a:30s} ; ./$cmd \' ' }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
cmd_stats_file := os.realpath([ c.workdir, 'v_performance_stats.json'].join(os.PathSeparator)) cmd_stats_file := os.realpath([ c.workdir, 'v_performance_stats.json'].join(os.path_separator))
comparison_cmd := 'hyperfine $c.hyperfineopts '+ comparison_cmd := 'hyperfine $c.hyperfineopts '+
'--export-json ${cmd_stats_file} '+ '--export-json ${cmd_stats_file} '+
'--time-unit millisecond '+ '--time-unit millisecond '+
@ -187,7 +187,7 @@ fn (c Context) compare_v_performance( commands []string ) {
fn (c Context) normalized_workpath_for_commit( commit string ) string { fn (c Context) normalized_workpath_for_commit( commit string ) string {
nc := 'v_at_' + commit.replace('^','_').replace('-','_').replace('/','_') nc := 'v_at_' + commit.replace('^','_').replace('-','_').replace('/','_')
return os.realpath( c.workdir + os.PathSeparator + nc ) return os.realpath( c.workdir + os.path_separator + nc )
} }
fn validate_commit_exists( commit string ){ fn validate_commit_exists( commit string ){

View File

@ -9,36 +9,36 @@ module math
// All functions are sorted alphabetically. // All functions are sorted alphabetically.
const ( const (
E = 2.71828182845904523536028747135266249775724709369995957496696763 e = 2.71828182845904523536028747135266249775724709369995957496696763
Pi = 3.14159265358979323846264338327950288419716939937510582097494459 pi = 3.14159265358979323846264338327950288419716939937510582097494459
Phi = 1.61803398874989484820458683436563811772030917980576286213544862 phi = 1.61803398874989484820458683436563811772030917980576286213544862
Tau = 6.28318530717958647692528676655900576839433879875021164194988918 tau = 6.28318530717958647692528676655900576839433879875021164194988918
Sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974
SqrtE = 1.64872127070012814684865078781416357165377610071014801157507931 sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931
SqrtPi = 1.77245385090551602729816748334114518279754945612238712821380779 sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779
SqrtTau = 2.50662827463100050241576528481104525300698674060993831662992357 sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357
SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038
Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
Log2E = 1.0 / Ln2 log2_e = 1.0 / ln2
Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 ln10 = 2.30258509299404568401799145468436420760110148862877297603332790
Log10E = 1.0 / Ln10 log10_e = 1.0 / ln10
) )
const ( const (
MaxI8 = 127 max_i8 = 127
MinI8 = -128 min_i8 = -128
MaxI16 = 32767 max_i16 = 32767
MinI16 = -32768 min_i16 = -32768
MaxI32 = 2147483647 max_i32 = 2147483647
MinI32 = -2147483648 min_i32 = -2147483648
// MaxI64 = ((1<<63) - 1) // MaxI64 = ((1<<63) - 1)
// MinI64 = (-(1 << 63) ) // MinI64 = (-(1 << 63) )
MaxU8 = 255 max_u8 = 255
MaxU16 = 65535 max_u16 = 65535
MaxU32 = 4294967295 max_u32 = 4294967295
MaxU64 = 18446744073709551615 max_u64 = 18446744073709551615
) )
// Returns the absolute value. // Returns the absolute value.
@ -93,7 +93,7 @@ pub fn cosh(a f64) f64 {
// degrees convert from degrees to radians. // degrees convert from degrees to radians.
pub fn degrees(radians f64) f64 { pub fn degrees(radians f64) f64 {
return radians * (180.0 / Pi) return radians * (180.0 / pi)
} }
// exp calculates exponent of the number (math.pow(math.E, a)). // exp calculates exponent of the number (math.pow(math.E, a)).
@ -277,7 +277,7 @@ pub fn pow(a, b f64) f64 {
// radians convert from radians to degrees. // radians convert from radians to degrees.
pub fn radians(degrees f64) f64 { pub fn radians(degrees f64) f64 {
return degrees * (Pi / 180.0) return degrees * (pi / 180.0)
} }
// round returns the integer nearest to the provided value. // round returns the integer nearest to the provided value.

View File

@ -485,7 +485,7 @@ pub fn dir(path string) string {
if path == '.' { if path == '.' {
return getwd() return getwd()
} }
pos := path.last_index(PathSeparator) pos := path.last_index(path_separator)
if pos == -1 { if pos == -1 {
return '.' return '.'
} }
@ -502,7 +502,7 @@ fn path_sans_ext(path string) string {
pub fn basedir(path string) string { pub fn basedir(path string) string {
pos := path.last_index(PathSeparator) pos := path.last_index(path_separator)
if pos == -1 { if pos == -1 {
return path return path
} }
@ -510,7 +510,7 @@ pub fn basedir(path string) string {
} }
pub fn filename(path string) string { pub fn filename(path string) string {
return path.all_after(PathSeparator) return path.all_after(path_separator)
} }
// get_line returns a one-line string from stdin // get_line returns a one-line string from stdin
@ -626,7 +626,7 @@ pub fn home_dir() string {
} }
home += homepath home += homepath
} }
home += PathSeparator home += path_separator
return home return home
} }
@ -795,7 +795,7 @@ pub fn walk_ext(path, ext string) []string {
if file.starts_with('.') { if file.starts_with('.') {
continue continue
} }
p := path + PathSeparator + file p := path + path_separator + file
if os.is_dir(p) { if os.is_dir(p) {
res << walk_ext(p, ext) res << walk_ext(p, ext)
} }
@ -859,9 +859,9 @@ pub fn print_backtrace() {
} }
pub fn mkdir_all(path string) { pub fn mkdir_all(path string) {
mut p := if path.starts_with(os.PathSeparator) { os.PathSeparator } else { '' } mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' }
for subdir in path.split(os.PathSeparator) { for subdir in path.split(os.path_separator) {
p += subdir + os.PathSeparator p += subdir + os.path_separator
if !os.dir_exists(p) { if !os.dir_exists(p) {
os.mkdir(p) os.mkdir(p)
} }

View File

@ -4,7 +4,6 @@ module os
#include <unistd.h> #include <unistd.h>
const ( const (
PathSeparator = '/'
path_separator = '/' path_separator = '/'
) )

View File

@ -4,7 +4,6 @@ module os
#include <winsock2.h> #include <winsock2.h>
const ( const (
PathSeparator = '\\'
path_separator = '\\' path_separator = '\\'
) )

View File

@ -7,7 +7,7 @@ module time
import rand import rand
const ( const (
MonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
) )
#include <time.h> #include <time.h>
@ -60,17 +60,17 @@ const (
// The unsigned zero year for internal calculations. // The unsigned zero year for internal calculations.
// Must be 1 mod 400, and times before it will not compute correctly, // Must be 1 mod 400, and times before it will not compute correctly,
// but otherwise can be changed at will. // but otherwise can be changed at will.
absoluteZeroYear = i64(-292277022399) absolute_zero_year = i64(-292277022399)
secondsPerMinute = 60 seconds_per_minute = 60
secondsPerHour = 60 * secondsPerMinute seconds_per_hour = 60 * seconds_per_minute
secondsPerDay = 24 * secondsPerHour seconds_per_day = 24 * seconds_per_hour
secondsPerWeek = 7 * secondsPerDay seconds_per_week = 7 * seconds_per_day
daysPer400Years = 365*400 + 97 days_per_400_years = 365*400 + 97
daysPer100Years = 365*100 + 24 days_per_100_years = 365*100 + 24
daysPer4Years = 365*4 + 1 days_per_4_years = 365*4 + 1
daysBefore = [ days_before = [
0, 0,
31, 31,
31 + 28, 31 + 28,
@ -88,33 +88,38 @@ const (
) )
const (
months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec'
days_string = 'MonTueWedThuFriSatSun'
)
// Based on Go's time package. // Based on Go's time package.
// Copyright 2009 The Go Authors. // Copyright 2009 The Go Authors.
pub fn unix(abs int) Time { pub fn unix(abs int) Time {
// Split into time and day. // Split into time and day.
mut d := abs / secondsPerDay mut d := abs / seconds_per_day
// Account for 400 year cycles. // Account for 400 year cycles.
mut n := d / daysPer400Years mut n := d / days_per_400_years
mut y := 400 * n mut y := 400 * n
d -= daysPer400Years * n d -= days_per_400_years * n
// Cut off 100-year cycles. // Cut off 100-year cycles.
// The last cycle has one extra leap year, so on the last day // The last cycle has one extra leap year, so on the last day
// of that year, day / daysPer100Years will be 4 instead of 3. // of that year, day / days_per_100_years will be 4 instead of 3.
// Cut it back down to 3 by subtracting n>>2. // Cut it back down to 3 by subtracting n>>2.
n = d / daysPer100Years n = d / days_per_100_years
n -= n >> 2 n -= n >> 2
y += 100 * n y += 100 * n
d -= daysPer100Years * n d -= days_per_100_years * n
// Cut off 4-year cycles. // Cut off 4-year cycles.
// The last cycle has a missing leap year, which does not // The last cycle has a missing leap year, which does not
// affect the computation. // affect the computation.
n = d / daysPer4Years n = d / days_per_4_years
y += 4 * n y += 4 * n
d -= daysPer4Years * n d -= days_per_4_years * n
// Cut off years within a 4-year cycle. // Cut off years within a 4-year cycle.
// The last year is a leap year, so on the last day of that year, // The last year is a leap year, so on the last day of that year,
@ -128,10 +133,10 @@ pub fn unix(abs int) Time {
yday := int(d) yday := int(d)
mut day := yday mut day := yday
year := abs / int(3.154e+7) + 1970 //int(i64(y) + absoluteZeroYear) year := abs / int(3.154e+7) + 1970 //int(i64(y) + absolute_zero_year)
hour := int(abs%secondsPerDay) / secondsPerHour hour := int(abs%seconds_per_day) / seconds_per_hour
minute := int(abs % secondsPerHour) / secondsPerMinute minute := int(abs % seconds_per_hour) / seconds_per_minute
second := int(abs % secondsPerMinute) second := int(abs % seconds_per_minute)
if is_leap_year(year) { if is_leap_year(year) {
// Leap year // Leap year
@ -149,12 +154,12 @@ pub fn unix(abs int) Time {
// The estimate may be too low by at most one month, so adjust. // The estimate may be too low by at most one month, so adjust.
mut month := day / 31 mut month := day / 31
mut begin := 0 mut begin := 0
end := int(daysBefore[month+1]) end := int(days_before[month+1])
if day >= end { if day >= end {
month++ month++
begin = end begin = end
} else { } else {
begin = int(daysBefore[month]) begin = int(days_before[month])
} }
month++ // because January is 1 month++ // because January is 1
@ -182,14 +187,10 @@ pub fn (t Time) format() string {
return '${t.year}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}' return '${t.year}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}'
} }
const (
Months = 'JanFebMarAprMayJunJulAugSepOctNovDec'
Days = 'MonTueWedThuFriSatSun'
)
pub fn (t Time) smonth() string { pub fn (t Time) smonth() string {
i := t.month - 1 i := t.month - 1
return Months.substr(i * 3, (i + 1) * 3) return months_string.substr(i * 3, (i + 1) * 3)
} }
// 21:04 // 21:04
@ -386,7 +387,7 @@ pub fn (t Time) day_of_week() int {
// weekday_str() returns the current day in string (upto 3 characters) // weekday_str() returns the current day in string (upto 3 characters)
pub fn (t Time) weekday_str() string { pub fn (t Time) weekday_str() string {
i := t.day_of_week() - 1 i := t.day_of_week() - 1
return Days.substr(i * 3, (i + 1) * 3) return days_string.substr(i * 3, (i + 1) * 3)
} }
struct C.timeval { struct C.timeval {
@ -450,6 +451,6 @@ pub fn days_in_month(month, year int) ?int {
return error('Invalid month: $month') return error('Invalid month: $month')
} }
extra := if month == 2 && is_leap_year(year) {1} else {0} extra := if month == 2 && is_leap_year(year) {1} else {0}
res := MonthDays[month-1] + extra res := month_days[month-1] + extra
return res return res
} }