-
Notifications
You must be signed in to change notification settings - Fork 55
Compiler: move c2_assert to c2 #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| module c2_assert; | ||
|
|
||
| import stdio local; | ||
| import stdlib local; | ||
|
|
||
| public fn i32 c2_assert_fail(const char* filename @(auto_file), | ||
| u32 line @(auto_line), | ||
| const char* funcname @(auto_func), | ||
| const char* condstr) | ||
| { | ||
| dprintf(2, "%s:%d: function %s: Assertion failed: %s\n", filename, line, funcname, condstr); | ||
| abort(); | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,4 +40,4 @@ modules: | |
| - sys_utsname | ||
| - uio | ||
| - unistd | ||
|
|
||
| - c2_assert | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import constants local; | |
| import token local; | ||
| import src_loc local; | ||
| import string_list; | ||
| import string_buffer; | ||
|
|
||
| fn Stmt* Parser.parseStmt(Parser* p) { | ||
| // TODO use Jump Table (combined one for multiple purposes?) | ||
|
|
@@ -387,10 +388,36 @@ fn Stmt* Parser.parseAssertStmt(Parser* p) { | |
| SrcLoc loc = p.tok.loc; | ||
| p.consumeToken(); | ||
| p.expectAndConsume(Kind.LParen); | ||
| SrcLoc eloc = p.tok.loc; | ||
| Expr* inner = p.parseExpr(); | ||
| u32 elen = p.prev_loc - eloc; | ||
| p.expectAndConsume(Kind.RParen); | ||
| u32 loc_end = p.prev_loc; | ||
| p.expectAndConsume(Kind.Semicolon); | ||
| return p.builder.actOnAssertStmt(loc, inner); | ||
|
|
||
| Expr* call = nil; | ||
| if (p.has_asserts) { | ||
| // add c2_assert.fail func designator | ||
| Ref[2] ref; | ||
| ref[0].loc = loc; | ||
| ref[0].name_idx = p.c2_assert_idx; | ||
| ref[1].loc = loc; | ||
| ref[1].name_idx = p.c2_assert_fail_idx; | ||
| p.addImplicitImport(p.c2_assert_idx, false); | ||
| Expr* func = p.builder.actOnMemberExpr(nil, ref, 2); | ||
|
|
||
| // encode expression as a string | ||
| string_buffer.Buf* buf = p.tokenizer.buf; | ||
| buf.clear(); | ||
| inner.printLiteral(buf); | ||
| u32 msg_len = buf.size(); | ||
| u32 msg_idx = p.pool.add(buf.data(), msg_len, true); | ||
| Expr* arg = p.builder.actOnStringLiteral(eloc, elen, msg_idx, msg_len); | ||
|
|
||
| // create call expression `assert.fail("expression") | ||
| call = p.builder.actOnCallExpr(loc, loc_end, func, &arg, 1); | ||
| } | ||
| return p.builder.actOnAssertStmt(loc, inner, call); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parser now insert implicit Imports. I dont really like that. The parser should convert the tokens into the AST. The analyser can then do its thing. This breaks that division...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is sloppy. I suggest this:
|
||
| } | ||
|
|
||
| fn Stmt* Parser.parseBreakStmt(Parser* p) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In which case will a.getCall() be nil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the call expression is only constructed by the parser if asserts are enabled. The expression is always parsed and stored for the analyser.