mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
New failable based error handling. Labelled break/continue/next.
This commit is contained in:
committed by
Christoffer Lerno
parent
10f715cec0
commit
dc86c21210
@@ -58,8 +58,6 @@ void comment(void);
|
||||
"void" { count(); return(VOID); }
|
||||
"volatile" { count(); return(VOLATILE); }
|
||||
"while" { count(); return(WHILE); }
|
||||
"throw" { count(); return(THROW); }
|
||||
"throws" { count(); return(THROWS); }
|
||||
"func" { count(); return(FUNC); }
|
||||
"nil" { count(); return(NIL); }
|
||||
"next" { count(); return(NEXT); }
|
||||
|
||||
11
resources/c3ir.md
Normal file
11
resources/c3ir.md
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
```
|
||||
block
|
||||
{
|
||||
z = add(x, y)
|
||||
z = sub(x, y)
|
||||
z = mult(x, y)
|
||||
neg(z, x)
|
||||
goto
|
||||
|
||||
```
|
||||
@@ -8,10 +8,10 @@ func int main()
|
||||
}
|
||||
}
|
||||
|
||||
func string@ bin(int x)
|
||||
func string bin(int x)
|
||||
{
|
||||
int bits = (x == 0) ? 1 : log10(cast(x, double)) / log10(2);
|
||||
string@ ret = string.make_repeat('0', bits);
|
||||
string ret = str.make_repeat('0', bits);
|
||||
for (int i = 0; i < bits; i++)
|
||||
{
|
||||
ret[bits - i - 1] = x & 1 ? '1' : '0';
|
||||
|
||||
@@ -21,12 +21,7 @@ func void main()
|
||||
{
|
||||
string s = match.string;
|
||||
printf("Enter a value for '%s': ", s.slice(1, s.size - 2));
|
||||
string! word = strin.readln().strip();
|
||||
catch (word)
|
||||
{
|
||||
// Exit on error.
|
||||
return;
|
||||
}
|
||||
string word = strin.readln().strip() else return;
|
||||
story = story.replace(s, word);
|
||||
}
|
||||
|
||||
|
||||
@@ -110,7 +110,7 @@ struct Parser
|
||||
/**
|
||||
* @ensure const(input)
|
||||
*/
|
||||
func Parser.parse(Parser* p, char* input, char* diagMsg, Blocks* blocks) throws
|
||||
func void! Parser.parse(Parser* p, char* input, char* diagMsg, Blocks* blocks)
|
||||
{
|
||||
p.tokenizer.init(input);
|
||||
p.tok.init();
|
||||
@@ -125,15 +125,9 @@ func Parser.parse(Parser* p, char* input, char* diagMsg, Blocks* blocks) throws
|
||||
|
||||
try p.consumeToken();
|
||||
try p.parseTopLevel();
|
||||
return true;
|
||||
|
||||
catch (error e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
func void Parser.parseTopLevel(Parser* p) throws ParseError
|
||||
func void! Parser.parseTopLevel(Parser* p)
|
||||
{
|
||||
// key = value
|
||||
// | [[array]]
|
||||
@@ -150,7 +144,7 @@ func void Parser.parseTopLevel(Parser* p) throws ParseError
|
||||
p.parseTableArray();
|
||||
default:
|
||||
sprintf(p.errorMsg, "syntax error %s", p.tok.loc.str());
|
||||
throw ParseError.SYNTAX_ERROR;
|
||||
return SyntaxError!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ void yyerror(char *s);
|
||||
%token ADD_MOD SUB_MOD MULT_MOD ADD_MOD_ASSIGN SUB_MOD_ASSIGN
|
||||
%token MULT_MOD_ASSIGN NEG_MOD
|
||||
%token XOR_ASSIGN OR_ASSIGN VAR NIL ELVIS HASH_IDENT NEXT
|
||||
%token NOFAIL_ASSIGN
|
||||
|
||||
%token TYPEDEF MODULE IMPORT
|
||||
%token CHAR SHORT INT LONG FLOAT DOUBLE CONST VOLATILE VOID
|
||||
@@ -402,14 +403,25 @@ expression_statement
|
||||
;
|
||||
|
||||
|
||||
if_expr
|
||||
: failable_type IDENT '=' initializer
|
||||
| failable_type IDENT NOFAIL_ASSIGN expression
|
||||
| expression
|
||||
;
|
||||
|
||||
if_cond_expr
|
||||
: if_expr
|
||||
| if_cond_expr ',' if_expr
|
||||
;
|
||||
|
||||
control_expression
|
||||
: decl_expr_list
|
||||
| decl_expr_list ';' decl_expr_list
|
||||
;
|
||||
|
||||
selection_statement
|
||||
: IF '(' control_expression ')' statement
|
||||
| IF '(' control_expression ')' compound_statement ELSE statement
|
||||
: IF '(' if_cond_expr ')' statement
|
||||
| IF '(' if_cond_expr ')' compound_statement ELSE statement
|
||||
| SWITCH '(' control_expression ')' compound_statement
|
||||
;
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
30
resources/testfragments/test_break.c3
Normal file
30
resources/testfragments/test_break.c3
Normal file
@@ -0,0 +1,30 @@
|
||||
module foo;
|
||||
|
||||
extern func void printf(char *hello, ...);
|
||||
|
||||
error MyError;
|
||||
error YourError;
|
||||
func void main()
|
||||
{
|
||||
int! i = 1;
|
||||
catch (i)
|
||||
{
|
||||
printf("Caught\n");
|
||||
return;
|
||||
}
|
||||
i = MyError!;
|
||||
catch FOO: (i)
|
||||
{
|
||||
case YourError:
|
||||
printf("YourError\n");
|
||||
case MyError:
|
||||
printf("MyError\n");
|
||||
next FOO : YourError;
|
||||
default:
|
||||
printf("Any error\n");
|
||||
}
|
||||
do
|
||||
{
|
||||
printf("Test\n");
|
||||
}
|
||||
}
|
||||
@@ -782,6 +782,7 @@ int i = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
func void test2(int* x, int y, int z)
|
||||
{
|
||||
*(&(&x)[0]);
|
||||
|
||||
Reference in New Issue
Block a user