Remove var and const keywords; Fix default parameter syntax

This commit is contained in:
Ginger Bill
2017-06-28 23:55:40 +01:00
parent 94afcec757
commit fd81c06c35
5 changed files with 29 additions and 62 deletions
+5 -5
View File
@@ -125,8 +125,8 @@ named_arguments :: proc() {
// Named arguments can also aid with default arguments // Named arguments can also aid with default arguments
numerous_things :: proc(s: string, a = 1, b = 2, c = 3.14, numerous_things :: proc(s: string, a := 1, b := 2, c := 3.14,
d = "The Best String!", e = false, f = 10.3/3.1, g = false) { d := "The Best String!", e := false, f := 10.3/3.1, g := false) {
g_str := g ? "true" : "false"; g_str := g ? "true" : "false";
fmt.printf("How many?! %s: %v\n", s, g_str); fmt.printf("How many?! %s: %v\n", s, g_str);
} }
@@ -147,7 +147,7 @@ named_arguments :: proc() {
default_return_values :: proc() { default_return_values :: proc() {
foo :: proc(x: int) -> (first: string = "Hellope", second = "world!") { foo :: proc(x: int) -> (first: string = "Hellope", second := "world!") {
match x { match x {
case 0: return; case 0: return;
case 1: return "Goodbye"; case 1: return "Goodbye";
@@ -178,7 +178,7 @@ default_return_values :: proc() {
id: u32, id: u32,
} }
some_thing :: proc(input: int) -> (result: ^Entity = nil, err = Error.None) { some_thing :: proc(input: int) -> (result: ^Entity = nil, err := Error.None) {
match { match {
case input == 3: return err = Error.WhyTheNumberThree; case input == 3: return err = Error.WhyTheNumberThree;
case input >= 10: return err = Error.TenIsTooBig; case input >= 10: return err = Error.TenIsTooBig;
@@ -192,7 +192,7 @@ default_return_values :: proc() {
} }
call_location :: proc() { call_location :: proc() {
amazing :: proc(n: int, using loc = #caller_location) { amazing :: proc(n: int, using loc := #caller_location) {
fmt.printf("%s(%d:%d) just asked to do something amazing.\n", fmt.printf("%s(%d:%d) just asked to do something amazing.\n",
fully_pathed_filename, line, column); fully_pathed_filename, line, column);
fmt.printf("Normal -> %d\n", n); fmt.printf("Normal -> %d\n", n);
+2 -2
View File
@@ -323,7 +323,7 @@ default_allocator :: proc() -> Allocator {
} }
assert :: proc(condition: bool, message = "", using location = #caller_location) -> bool #cc_contextless { assert :: proc(condition: bool, message := "", using location := #caller_location) -> bool #cc_contextless {
if !condition { if !condition {
if len(message) > 0 { if len(message) > 0 {
fmt.printf("%s(%d:%d) Runtime assertion: %s\n", fully_pathed_filename, line, column, message); fmt.printf("%s(%d:%d) Runtime assertion: %s\n", fully_pathed_filename, line, column, message);
@@ -335,7 +335,7 @@ assert :: proc(condition: bool, message = "", using location = #caller_location)
return condition; return condition;
} }
panic :: proc(message = "", using location = #caller_location) #cc_contextless { panic :: proc(message := "", using location := #caller_location) #cc_contextless {
if len(message) > 0 { if len(message) > 0 {
fmt.printf("%s(%d:%d) Panic: %s\n", fully_pathed_filename, line, column, message); fmt.printf("%s(%d:%d) Panic: %s\n", fully_pathed_filename, line, column, message);
} else { } else {
+2 -6
View File
@@ -1654,12 +1654,8 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
for_array(i, fb->decls) { for_array(i, fb->decls) {
AstNode *decl = fb->decls[i]; AstNode *decl = fb->decls[i];
if (decl->kind == AstNode_GenDecl) { if (decl->kind == AstNode_ValueDecl && decl->ValueDecl.is_mutable) {
switch (decl->GenDecl.token.kind) { check_stmt(c, decl, flags);
case Token_var:
check_stmt(c, decl, flags);
break;
}
} }
} }
+20 -47
View File
@@ -1711,10 +1711,6 @@ void fix_advance_to_next_stmt(AstFile *f) {
case Token_Semicolon: case Token_Semicolon:
return; return;
case Token_var:
case Token_const:
case Token_type:
case Token_proc:
case Token_foreign: case Token_foreign:
case Token_foreign_library: case Token_foreign_library:
case Token_foreign_system_library: case Token_foreign_system_library:
@@ -1857,15 +1853,6 @@ void expect_semicolon(AstFile *f, AstNode *s) {
String node_string = ast_node_strings[s->kind]; String node_string = ast_node_strings[s->kind];
if (s->kind == AstNode_GenDecl) { if (s->kind == AstNode_GenDecl) {
switch (s->GenDecl.token.kind) { switch (s->GenDecl.token.kind) {
case Token_var:
node_string = str_lit("variable declaration");
break;
case Token_const:
node_string = str_lit("const declaration");
break;
case Token_type:
node_string = str_lit("type declaration");
break;
case Token_import: case Token_import:
case Token_import_load: case Token_import_load:
node_string = str_lit("import declaration"); node_string = str_lit("import declaration");
@@ -2863,14 +2850,6 @@ void parse_foreign_block_decl(AstFile *f, Array<AstNode *> *decls) {
array_add(decls, decl); array_add(decls, decl);
return; return;
case AstNode_GenDecl:
switch (decl->GenDecl.token.kind) {
case Token_var:
array_add(decls, decl);
return;
}
/* fallthrough */ /* fallthrough */
default: default:
error(decl, "Foreign blocks only allow procedure and variable declarations"); error(decl, "Foreign blocks only allow procedure and variable declarations");
@@ -3329,8 +3308,7 @@ AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, Tok
next_token(f); next_token(f);
} }
if (f->curr_token.kind == Token_Colon || if (f->curr_token.kind == Token_Colon) {
f->curr_token.kind == Token_Eq) {
Array<AstNode *> names = convert_to_ident_list(f, list, true); // Copy for semantic reasons Array<AstNode *> names = convert_to_ident_list(f, list, true); // Copy for semantic reasons
if (names.count == 0) { if (names.count == 0) {
syntax_error(f->curr_token, "Empty field declaration"); syntax_error(f->curr_token, "Empty field declaration");
@@ -3345,8 +3323,8 @@ AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, Tok
AstNode *type = NULL; AstNode *type = NULL;
AstNode *default_value = NULL; AstNode *default_value = NULL;
expect_token_after(f, Token_Colon, "field list");
if (f->curr_token.kind != Token_Eq) { if (f->curr_token.kind != Token_Eq) {
expect_token_after(f, Token_Colon, "field list");
type = parse_var_type(f, allow_ellipsis, allow_type_token); type = parse_var_type(f, allow_ellipsis, allow_type_token);
} }
if (allow_token(f, Token_Eq)) { if (allow_token(f, Token_Eq)) {
@@ -3381,8 +3359,8 @@ AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, Tok
AstNode *type = NULL; AstNode *type = NULL;
AstNode *default_value = NULL; AstNode *default_value = NULL;
expect_token_after(f, Token_Colon, "field list");
if (f->curr_token.kind != Token_Eq) { if (f->curr_token.kind != Token_Eq) {
expect_token_after(f, Token_Colon, "field list");
type = parse_var_type(f, allow_ellipsis, allow_default_parameters); type = parse_var_type(f, allow_ellipsis, allow_default_parameters);
} }
if (allow_token(f, Token_Eq)) { if (allow_token(f, Token_Eq)) {
@@ -4174,36 +4152,31 @@ AstNode *parse_stmt(AstFile *f) {
} }
case Token_using: { case Token_using: {
// TODO(bill): Make using statements better CommentGroup docs = f->lead_comment;
Token token = expect_token(f, Token_using); Token token = expect_token(f, Token_using);
AstNode *decl = NULL; AstNode *decl = NULL;
if (f->curr_token.kind == Token_var) { Array<AstNode *> list = parse_lhs_expr_list(f);
decl = parse_decl(f); if (list.count == 0) {
expect_semicolon(f, decl); syntax_error(token, "Illegal use of `using` statement");
} else { expect_semicolon(f, NULL);
Array<AstNode *> list = parse_lhs_expr_list(f); return ast_bad_stmt(f, token, f->curr_token);
if (list.count == 0) {
syntax_error(token, "Illegal use of `using` statement");
expect_semicolon(f, NULL);
return ast_bad_stmt(f, token, f->curr_token);
}
if (f->curr_token.kind != Token_Colon) {
expect_semicolon(f, list[list.count-1]);
return ast_using_stmt(f, token, list);
}
} }
if (f->curr_token.kind != Token_Colon) {
expect_semicolon(f, list[list.count-1]);
return ast_using_stmt(f, token, list);
}
decl = parse_value_decl(f, list, docs);
if (decl != NULL && decl->kind == AstNode_GenDecl) { if (decl != NULL && decl->kind == AstNode_ValueDecl) {
if (decl->GenDecl.token.kind != Token_var) { if (!decl->ValueDecl.is_mutable) {
syntax_error(token, "`using` may only be applied to variable declarations"); syntax_error(token, "`using` may only be applied to variable declarations");
return decl; return decl;
} }
if (f->curr_proc == NULL) { if (f->curr_proc == NULL) {
syntax_error(token, "`using` is not allowed at the file scope"); syntax_error(token, "`using` is not allowed at the file scope");
} else { } else {
decl->GenDecl.flags |= VarDeclFlag_using; decl->ValueDecl.flags |= VarDeclFlag_using;
} }
return decl; return decl;
} }
@@ -4271,14 +4244,14 @@ AstNode *parse_stmt(AstFile *f) {
} else if (tag == "thread_local") { } else if (tag == "thread_local") {
AstNode *s = parse_stmt(f); AstNode *s = parse_stmt(f);
if (s->kind == AstNode_GenDecl) { if (s->kind == AstNode_ValueDecl) {
if (s->GenDecl.token.kind != Token_var) { if (!s->ValueDecl.is_mutable) {
syntax_error(token, "`thread_local` may only be applied to variable declarations"); syntax_error(token, "`thread_local` may only be applied to variable declarations");
} }
if (f->curr_proc != NULL) { if (f->curr_proc != NULL) {
syntax_error(token, "`thread_local` is only allowed at the file scope"); syntax_error(token, "`thread_local` is only allowed at the file scope");
} else { } else {
s->GenDecl.flags |= VarDeclFlag_thread_local; s->ValueDecl.flags |= VarDeclFlag_thread_local;
} }
return s; return s;
} }
-2
View File
@@ -83,8 +83,6 @@ TOKEN_KIND(Token__ComparisonEnd, "_ComparisonEnd"), \
TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \ TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \
\ \
TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_var, "var"), \
TOKEN_KIND(Token_const, "const"), \
TOKEN_KIND(Token_type, "type"), \ TOKEN_KIND(Token_type, "type"), \
TOKEN_KIND(Token_import, "import"), \ TOKEN_KIND(Token_import, "import"), \
TOKEN_KIND(Token_import_load, "import_load"), \ TOKEN_KIND(Token_import_load, "import_load"), \