mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Updated examples and grammar for big error change.
This commit is contained in:
@@ -8,15 +8,12 @@ module acorn::arr;
|
||||
* See Copyright Notice in avm.h
|
||||
*/
|
||||
|
||||
error SearchError
|
||||
{
|
||||
NOT_FOUND
|
||||
}
|
||||
|
||||
/* Return a new Array, allocating len slots for Values. */
|
||||
func Value new(Value th, Value *dest, Value type, AuintIdx len)
|
||||
{
|
||||
// Create an array object
|
||||
ArrInfo* val = @cast(mem::new(th, ArrEnc, sizeof(ArrInfo), ArrInfo*);
|
||||
ArrInfo* val = cast(mem::new(th, ArrEnc, sizeof(ArrInfo), ArrInfo*);
|
||||
val.flags1 = 0; // Initialize Flags1 flags
|
||||
val.type = type;
|
||||
val.avail = len;
|
||||
@@ -30,7 +27,7 @@ func Value new(Value th, Value *dest, Value type, AuintIdx len)
|
||||
func Value newClosure(Value *th, Value *dest, Value type, AuintIdx len)
|
||||
{
|
||||
// Create an array object
|
||||
ArrInfo* val = @cast(mem::new(th, ArrEnc, sizeof(ArrInfo), ArrInfo*);
|
||||
ArrInfo* val = cast(mem::new(th, ArrEnc, sizeof(ArrInfo), ArrInfo*);
|
||||
val.flags1 = TypeClo; // Initialize Flags1 flags
|
||||
val.type = type;
|
||||
val.avail = len;
|
||||
|
||||
@@ -14,18 +14,16 @@ module acorn::mem;
|
||||
* It returns the location of the new block or NULL (if freed). */
|
||||
func void* gcrealloc(Value th, void *block, Auint osize, Auint nsize)
|
||||
{
|
||||
Value newblock;
|
||||
|
||||
// Check consistency of block and osize (both must be null or specified)
|
||||
Auint realosize = block ? osize : 0;
|
||||
assert((realosize == 0) == (block == nil));
|
||||
|
||||
// Allocate/free/resize the memory block
|
||||
newblock = @cast(frealloc(block, nsize), Value);
|
||||
Value newblock = cast(frealloc(block, nsize), Value);
|
||||
|
||||
$if (defined(MEMORYLOG))
|
||||
{
|
||||
if (nsize==0)
|
||||
if (nsize == 0)
|
||||
{
|
||||
vmLog("Freeing %p size %d", block, osize);
|
||||
}
|
||||
@@ -40,7 +38,7 @@ func void* gcrealloc(Value th, void *block, Auint osize, Auint nsize)
|
||||
{
|
||||
// realloc cannot fail when shrinking a block
|
||||
gcfull(th, 1); // try to free some memory...
|
||||
newblock = @cast(frealloc(block, nsize), Value); // try again
|
||||
newblock = cast(frealloc(block, nsize), Value); // try again
|
||||
if (newblock == nil)
|
||||
{
|
||||
logSevere("Out of memory trying allocate or grow a memory block.");
|
||||
@@ -57,7 +55,7 @@ func void* gcreallocv(Value th, void* block, Auint osize, Auint nsize, Auint esi
|
||||
{
|
||||
// Ensure we are not asking for more memory than available in address space
|
||||
// If we do not do this, calculating the needed memory will overflow
|
||||
if (nsize+1 > ~((Auint)0) / esize)
|
||||
if (nsize + 1 > ~(cast(0, Auint)) / esize)
|
||||
{
|
||||
logSevere("Out of memory trying to ask for more memory than address space has.");
|
||||
}
|
||||
@@ -72,20 +70,17 @@ func void* gcreallocv(Value th, void* block, Auint osize, Auint nsize, Auint esi
|
||||
**/
|
||||
func void* frealloc(void* block, Auint size)
|
||||
{
|
||||
if (size == 0)
|
||||
if (!size)
|
||||
{
|
||||
free(block);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return realloc(block, size);
|
||||
}
|
||||
return realloc(block, size);
|
||||
}
|
||||
|
||||
macro type($type) @amalloc($type)
|
||||
{
|
||||
return @cast(mem_frealloc(NULL, sizeof($type)), $type);
|
||||
return cast(mem_frealloc(NULL, sizeof($type)), $type);
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +98,7 @@ MemInfo* new(Value th, int enc, Auint sz)
|
||||
gccheck(th); // Incremental GC before memory allocation events
|
||||
}
|
||||
vm(th).gcnbrnew++;
|
||||
MemInfo* o = (MemInfo*) (char *) gcrealloc(th, nil, 0, sz);
|
||||
MemInfo* o = cast(gcrealloc(th, nil, 0, sz), MemInfo *);
|
||||
o.marked = vm(th).currentwhite & WHITEBITS;
|
||||
o.enctyp = enc;
|
||||
|
||||
@@ -132,7 +127,7 @@ func MemInfo* newnolink(Value th, int enc, Auint sz)
|
||||
}
|
||||
vm(th)->gcnbrnew++;
|
||||
// Allocate and initialize
|
||||
MemInfo *o = (MemInfo*) (char *) gcrealloc(th, NULL, 0, sz);
|
||||
MemInfo *o = cast(gcrealloc(th, NULL, 0, sz), MemInfo*);
|
||||
o.marked = vm(th)->currentwhite & WHITEBITS;
|
||||
o.enctyp = enc;
|
||||
return o;
|
||||
@@ -142,7 +137,7 @@ func MemInfo* newnolink(Value th, int enc, Auint sz)
|
||||
func void growaux_(Value th, void *block, AuintIdx *size, AuintIdx size_elems, AuintIdx limit)
|
||||
{
|
||||
void* newblock;
|
||||
AuintIdx newsize;
|
||||
AuintIdx newsize = void;
|
||||
// cannot double it?
|
||||
if (*size >= limit / 2)
|
||||
{
|
||||
|
||||
@@ -82,7 +82,13 @@ public func void encode(byte[] in, string *out)
|
||||
}
|
||||
}
|
||||
|
||||
public func int decode(string in, byte[] out) throws Base64Error
|
||||
error InvalidCharacter
|
||||
{
|
||||
int index;
|
||||
char c;
|
||||
}
|
||||
|
||||
public func int! decode(string in, byte[] out)
|
||||
{
|
||||
int j = 0;
|
||||
|
||||
@@ -92,9 +98,9 @@ public func int decode(string in, byte[] out) throws Base64Error
|
||||
|
||||
if (value == PAD) return j;
|
||||
|
||||
if (value < FIRST || in[i] > LAST) throw INVALID_CHARACTER;
|
||||
if (value < FIRST || in[i] > LAST) return! InvalidCharacter(i, value);
|
||||
byte c = LUT_DEC[in[i] - FIRST);
|
||||
if (c == ERR) throw INVALID_CHARACTER;
|
||||
if (c == ERR) return! InvalidCharacter(i, value);
|
||||
|
||||
switch (i % 4)
|
||||
{
|
||||
|
||||
@@ -4,9 +4,7 @@ func int main(void)
|
||||
{
|
||||
Curl curl;
|
||||
|
||||
try curl.init();
|
||||
|
||||
catch (error e)
|
||||
catch (e = curl.init())
|
||||
{
|
||||
printf("Failed to create new curl: %s\n", e.message);
|
||||
exit(FAILURE);
|
||||
@@ -15,9 +13,7 @@ func int main(void)
|
||||
curl.setopt(URL, "http://www.rosettacode.org/");
|
||||
curl.setopt(FOLLOWLOCATION, 1);
|
||||
|
||||
try curl.perform();
|
||||
|
||||
catch (CurlError e)
|
||||
catch (e = curl.perform())
|
||||
{
|
||||
printf("Error making request: %s\n", e.message);
|
||||
exit(FAILURE);
|
||||
|
||||
@@ -7,7 +7,7 @@ func void main()
|
||||
string story = "";
|
||||
while (1)
|
||||
{
|
||||
try string line = stdin.readln().strip();
|
||||
string line = stdin.readln().strip() else "";
|
||||
if (!line.size) break;
|
||||
story += line + "\n";
|
||||
}
|
||||
@@ -21,14 +21,14 @@ func void main()
|
||||
{
|
||||
string s = match.string;
|
||||
printf("Enter a value for '%s': ", s.slice(1, s.size - 2));
|
||||
try string word = strin.readln().strip();
|
||||
string! word = strin.readln().strip();
|
||||
catch (word)
|
||||
{
|
||||
// Exit on error.
|
||||
return;
|
||||
}
|
||||
story = story.replace(s, word);
|
||||
}
|
||||
|
||||
println("\nThe story becomes:\n%s\n", story);
|
||||
|
||||
catch (error e)
|
||||
{
|
||||
// Ignore any error
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ const uint MaxDepth = 8;
|
||||
|
||||
$if (DEBUG_NODES):
|
||||
|
||||
func void Blocks.dump(const Blocks* b)
|
||||
func void Blocks.dump(Blocks* b)
|
||||
{
|
||||
printf("Nodes (%u/%u) (%u bytes)\n", b.nodeCount, b.maxNodes, b.nodeCount * sizeof(Node));
|
||||
for (uint i = 0; i < b.nodeCount; i++)
|
||||
@@ -170,7 +170,7 @@ func uint addType(uint raw, ValueType t) @(inline)
|
||||
return raw | (t << ValueTypeOffset);
|
||||
}
|
||||
|
||||
func void Parser.parseKeyValue(Parser* p) throws ParseError
|
||||
func void! Parser.parseKeyValue(Parser* p)
|
||||
{
|
||||
//printf("parseKeyValue()\n");
|
||||
char[MaxText] key;
|
||||
@@ -193,7 +193,7 @@ func void Parser.parseKeyValue(Parser* p) throws ParseError
|
||||
p.lastChild[p.numParents] = node;
|
||||
}
|
||||
|
||||
func void Parser.parseTable(Parser* p) throws ParseError
|
||||
func void! Parser.parseTable(Parser* p)
|
||||
{
|
||||
//printf("parseTable()\n");
|
||||
try p.consumeToken();
|
||||
@@ -348,13 +348,10 @@ func u32 Parser.addTable(Parser* p, const char* name, u32 depth, bool isTop, Nod
|
||||
return 0;
|
||||
}
|
||||
|
||||
func Location Parser.consumeToken(Parser* p) {
|
||||
func Location! Parser.consumeToken(Parser* p)
|
||||
{
|
||||
Location prev = p.tok.loc;
|
||||
p.tokenizer.lex(&p.tok);
|
||||
if (p.tok.is(TokenKind.Error)) {
|
||||
strcpy(p.errorMsg, p.tok.text);
|
||||
longjmp(p.jump_err, 1);
|
||||
}
|
||||
try p.tokenizer.lex(&p.tok);
|
||||
return prev;
|
||||
}
|
||||
|
||||
@@ -362,16 +359,19 @@ func Token* Parser.nextToken(Parser* p) {
|
||||
return p.tokenizer.lookahead();
|
||||
}
|
||||
|
||||
func void Parser.expectAndConsume(Parser* p, TokenKind k) {
|
||||
if (p.tok.isNot(k)) {
|
||||
func void! Parser.expectAndConsume(Parser* p, TokenKind k) {
|
||||
if (p.tok.isNot(k))
|
||||
{
|
||||
sprintf(p.errorMsg, "expected '%s' at %s", token2str(k), p.tok.loc.str());
|
||||
longjmp(p.jump_err, 1);
|
||||
}
|
||||
p.consumeToken();
|
||||
try p.consumeToken();
|
||||
}
|
||||
|
||||
func void Parser.expect(Parser* p, TokenKind k) {
|
||||
if (p.tok.isNot(k)) {
|
||||
func void Parser.expect(Parser* p, TokenKind k)
|
||||
{
|
||||
if (p.tok.isNot(k))
|
||||
{
|
||||
sprintf(p.errorMsg, "expected '%s' at %s", token2str(k), p.tok.loc.str());
|
||||
longjmp(p.jump_err, 1);
|
||||
}
|
||||
@@ -385,7 +385,7 @@ public struct TomlReader @opaque
|
||||
Blocks* blocks;
|
||||
}
|
||||
|
||||
public func TomlReader* TomlReader.create()
|
||||
public func TomlReader* new_toml()
|
||||
{
|
||||
TomlReader* r = @malloc(TomlReader);
|
||||
r.blocks = @malloc(Blocks);
|
||||
@@ -405,7 +405,9 @@ public func const char* TomlReader.getMsg(const TomlReader* r)
|
||||
return r.message;
|
||||
}
|
||||
|
||||
public func void TomlReader.parse(TomlReader* r, string filename) throws ParseError, FileError
|
||||
error EmptyFileError;
|
||||
|
||||
public func void! TomlReader.parse(TomlReader* r, string filename)
|
||||
{
|
||||
Reader file;
|
||||
|
||||
@@ -416,7 +418,7 @@ public func void TomlReader.parse(TomlReader* r, string filename) throws ParseEr
|
||||
if (file.isEmpty())
|
||||
{
|
||||
printf("file %s is empty\n", filename);
|
||||
throw ParseError.EMPTY_FILE;
|
||||
raise EmptyFileError;
|
||||
}
|
||||
|
||||
Parser parser;
|
||||
@@ -431,7 +433,8 @@ $endif
|
||||
// --------------------------------------------------------------
|
||||
// Getters+iters
|
||||
|
||||
func const Node* Reader.findNode(const Reader* r, const char* key) {
|
||||
func const Node* Reader.findNode(const Reader* r, const char* key)
|
||||
{
|
||||
char[MaxText] name;
|
||||
const char* cp = key;
|
||||
const char* start = cp;
|
||||
|
||||
@@ -103,7 +103,13 @@ func void Tokenizer.init(Tokenizer* t, char* input)
|
||||
t.text[0] = 0;
|
||||
}
|
||||
|
||||
func void Tokenizer.lex(Tokenizer* t, Token* result)
|
||||
error LexedEOF;
|
||||
error LexError
|
||||
{
|
||||
string error_message;
|
||||
}
|
||||
|
||||
func void! Tokenizer.lex(Tokenizer* t, Token* result)
|
||||
{
|
||||
if (t.haveNext)
|
||||
{
|
||||
@@ -118,16 +124,12 @@ func void Tokenizer.lex(Tokenizer* t, Token* result)
|
||||
switch (t.current[0])
|
||||
{
|
||||
case 0:
|
||||
result.loc = t.loc;
|
||||
result.kind = TokenKind.EOF;
|
||||
return;
|
||||
return! LexedEOF();
|
||||
case '#':
|
||||
if (t.loc.column != 1)
|
||||
{
|
||||
sprintf(t.text, "unexpected '#' after line start at %s", t.loc.str());
|
||||
result.kind = TokenKind.ERROR;
|
||||
result.text = t.text;
|
||||
return;
|
||||
return! LexError(t.text);
|
||||
}
|
||||
t.parseComment();
|
||||
case ' ':
|
||||
@@ -214,18 +216,16 @@ func void Tokenizer.lex(Tokenizer* t, Token* result)
|
||||
return;
|
||||
}
|
||||
sprintf(t.text, "unexpected char '%c' at %s", t.current[0], t.loc.str());
|
||||
result.kind = ERROR;
|
||||
result.text = t.text;
|
||||
return;
|
||||
return LexError(t.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Token* Tokenizer.lookahead(Tokenizer* t)
|
||||
func Token*! Tokenizer.lookahead(Tokenizer* t)
|
||||
{
|
||||
if (!t.haveNext)
|
||||
{
|
||||
t.lex(&t.nextToken);
|
||||
try t.lex(&t.nextToken);
|
||||
t.haveNext = true;
|
||||
}
|
||||
return &t.nextToken;
|
||||
@@ -277,7 +277,7 @@ func void Tokenizer.parseText(Tokenizer* t, Token* result)
|
||||
t.advance(1);
|
||||
}
|
||||
|
||||
func void Tokenizer.parseMultiText(Tokenizer* t, Token* result)
|
||||
func void! Tokenizer.parseMultiText(Tokenizer* t, Token* result)
|
||||
{
|
||||
t.advance(3);
|
||||
if (t.current[0] == '\n')
|
||||
@@ -293,9 +293,7 @@ func void Tokenizer.parseMultiText(Tokenizer* t, Token* result)
|
||||
if (t.current[0] == 0)
|
||||
{
|
||||
sprintf(t.text, "missing end \"\"\" %s", t.loc.str());
|
||||
result.kind = TokenKind.Error;
|
||||
result.text = t.text;
|
||||
return;
|
||||
return! LexError(t.text);
|
||||
}
|
||||
if (t.current[0] == '\n')
|
||||
{
|
||||
@@ -310,7 +308,7 @@ func void Tokenizer.parseMultiText(Tokenizer* t, Token* result)
|
||||
t.current++;
|
||||
}
|
||||
|
||||
uint len = cast(uint, t.current - start);
|
||||
uint len = uint(t.current - start);
|
||||
// assert(len < MaxText);
|
||||
memcpy(t.text, start, len);
|
||||
t.text[len] = 0;
|
||||
@@ -325,7 +323,7 @@ func void Tokenizer.parseNumber(Tokenizer* t, Token* result)
|
||||
// handle hexadecimal/ocal/binary number
|
||||
// handle '_', like 1_000_000
|
||||
|
||||
uint number = cast(atoi(t.current), uint);
|
||||
uint number = uint(atoi(t.current));
|
||||
result.kind = TokenKind.Number;
|
||||
result.number = number;
|
||||
while (t.current[0] && isdigit(t.current[0]))
|
||||
|
||||
Reference in New Issue
Block a user