public enums + more public structs (libs/examples/tests)

pull/2519/head
Alexander Medvednikov 2019-10-24 13:19:27 +03:00
parent 1a2db54bc1
commit 3d6bdc2dc3
10 changed files with 23 additions and 13 deletions

View File

@ -6,6 +6,12 @@ module compiler
fn (p mut Parser) enum_decl(_enum_name string) { fn (p mut Parser) enum_decl(_enum_name string) {
mut enum_name := _enum_name mut enum_name := _enum_name
is_pub := p.tok == .key_pub
if is_pub {
p.next()
p.check(.key_enum)
enum_name = p.check_name()
}
// Specify full type name // Specify full type name
if !p.builtin_mod && p.mod != 'main' { if !p.builtin_mod && p.mod != 'main' {
enum_name = p.prepend_mod(enum_name) enum_name = p.prepend_mod(enum_name)
@ -49,6 +55,7 @@ fn (p mut Parser) enum_decl(_enum_name string) {
parent: 'int' parent: 'int'
cat: TypeCategory.enum_ cat: TypeCategory.enum_
enum_vals: fields.clone() enum_vals: fields.clone()
is_public: is_pub
}) })
p.check(.rcbr) p.check(.rcbr)
p.fgenln('\n') p.fgenln('\n')

View File

@ -320,10 +320,12 @@ fn (p mut Parser) parse(pass Pass) {
p.fgen(' ') p.fgen(' ')
p.enum_decl(name) p.enum_decl(name)
} }
// enum without a name, only allowed in code, translated from C
// it's a very bad practice in C as well, but is used unfortunately (for example, by DOOM)
// such fields are basically int consts
else if p.pref.translated { else if p.pref.translated {
// enum without a name, only allowed in code,
// translated from C. it's a very bad practice
// in C as well, but is used unfortunately
// (for example, by DOOM). such fields are
// basically int consts
p.enum_decl('int') p.enum_decl('int')
} }
else { else {
@ -335,6 +337,7 @@ fn (p mut Parser) parse(pass Pass) {
.key_fn { p.fn_decl() } .key_fn { p.fn_decl() }
.key_const { p.const_decl() } .key_const { p.const_decl() }
.key_struct { p.struct_decl() } .key_struct { p.struct_decl() }
.key_enum { p.enum_decl('') }
else { else {
p.error('wrong pub keyword usage') p.error('wrong pub keyword usage')
} }
@ -1101,7 +1104,7 @@ fn (p mut Parser) get_type() string {
} }
} }
else if !t.is_public && t.mod != p.mod && t.name != '' { else if !t.is_public && t.mod != p.mod && t.name != '' {
p.warn('type `$t.name` is private') p.error('type `$t.name` is private')
} }
} }
if typ == 'void' { if typ == 'void' {

View File

@ -1,6 +1,6 @@
module crypto module crypto
enum Hash { pub enum Hash {
MD4 MD4
MD5 MD5
SHA1 SHA1

View File

@ -10,7 +10,7 @@ import glm
// import darwin // import darwin
struct Shader { pub struct Shader {
program_id int program_id int
} }

View File

@ -9,7 +9,7 @@ module gl
#flag @VROOT/thirdparty/glad/glad.o #flag @VROOT/thirdparty/glad/glad.o
// joe-c: fix & remove // joe-c: fix & remove
enum TmpGlImportHack{} pub enum TmpGlImportHack{}
pub fn init_glad() { pub fn init_glad() {
ok := C.gladLoadGL() ok := C.gladLoadGL()

View File

@ -4,14 +4,14 @@
module gx module gx
struct Color { pub struct Color {
pub: pub:
r int r int
g int g int
b int b int
} }
const ( pub const (
// Primary colors // Primary colors
Blue = Color { r: 0, g: 0, b: 255 } Blue = Color { r: 0, g: 0, b: 255 }
Red = Color { r: 255, g: 0, b: 0 } Red = Color { r: 255, g: 0, b: 0 }

View File

@ -305,7 +305,7 @@ fn escape(s string, mode EncodingMode) string {
// //
// URL's String method uses the escaped_path method to obtain the path. See the // URL's String method uses the escaped_path method to obtain the path. See the
// escaped_path method for more details. // escaped_path method for more details.
struct URL { pub struct URL {
pub: mut: pub: mut:
scheme string scheme string
opaque string // encoded opaque data opaque string // encoded opaque data

View File

@ -5,7 +5,7 @@
module sync module sync
#include <pthread.h> #include <pthread.h>
struct Mutex { pub struct Mutex {
mutex C.pthread_mutex_t mutex C.pthread_mutex_t
} }

View File

@ -7,7 +7,7 @@ module sync
// Mutex HANDLE // Mutex HANDLE
type MHANDLE voidptr type MHANDLE voidptr
struct Mutex { pub struct Mutex {
mut: mut:
mx MHANDLE // mutex handle mx MHANDLE // mutex handle
state MutexState // mutex state state MutexState // mutex state

View File

@ -4,7 +4,7 @@
module sync module sync
struct WaitGroup { pub struct WaitGroup {
mut: mut:
mu Mutex mu Mutex
active int active int