Updated grammar. Removal of elif. Removal of ':' ';' in some ct statements. Empty faults is now an error. Remove "define" for types. Remove "private". Better errors on incorrect bitstruct syntax. Introduction of wildcard type rather than optional wildcard. Removal of scaled vector type. mkdir and rmdir. Disallow define @Foo() = { @inline }. Add handling for @optreturn and change it to @return!. Restrict interface style functions. Updated x64 ABI. stdlib updates to string. Removed deprecated functions. Update how variadics are implemented. Extended error messages. x86 ABI fixes. Shift check fixes. '!' and '?' are flipped. No trailing ',' allowed in functions. Fix to string parsing. Allow l suffix. Simplifying flatpath. any replaces variant, anyfault replaces anyerr. Allow getting the underlying type of anyfault. De-duplicate string constants. Fix of readme. Extended list. Fix of "(MyEnum)x + 1". Clock and DateTime types. Fixes to array concat.

This commit is contained in:
Christoffer Lerno
2023-03-23 14:49:51 +01:00
committed by Christoffer Lerno
parent d14e778232
commit 809321e20c
270 changed files with 8777 additions and 7237 deletions

View File

@@ -89,7 +89,7 @@ fn int! decode(String in, char* out, int* invalid_char_index = null)
if (c == ERR)
{
if (invalid_char_index) *invalid_char_index = i;
return DecodingError.INVALID_CHARACTER!;
return DecodingError.INVALID_CHARACTER?;
}
@@ -128,7 +128,7 @@ fn void! main()
printf("Result: %s\n", &buffer);
char *to_decode = "aGVsbG8gd29ybGRcMA==";
char[*] result = b64"aGVsbG8gd29ybGRcMA==";
decode((String)to_decode[0..19], &buffer)?;
decode((String)to_decode[0..19], &buffer)!;
printf("Result: %s\n", &buffer);
printf("Result direct: %.*s\n", 13, &result);
}

View File

@@ -12,7 +12,7 @@ fault InterpretError
fn void! print_error(usz pos, String err)
{
io::printfn("Error at %s: %s", pos, err);
return InterpretError.INTEPRET_FAILED!;
return InterpretError.INTEPRET_FAILED?;
}
fn void! brainf(String program)
@@ -81,5 +81,5 @@ fn void! main()
>>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
<<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-
]`;
brainf(program)?;
brainf(program)!;
}

View File

@@ -3,11 +3,11 @@ import libc;
import std::io;
struct Doc { Head *head; }
struct Head { VarString* title; }
struct Head { DString* title; }
struct Summary
{
VarString* title;
DString* title;
bool ok;
}
@@ -44,7 +44,7 @@ fn bool contains(String haystack, String needle)
macro @dupe(value)
{
$typeof(&value) temp = malloc_checked($typeof(value))?;
$typeof(&value) temp = malloc_checked($typeof(value))!;
*temp = value;
return temp;
}
@@ -56,11 +56,11 @@ fault ReadError
fn Doc! readDoc(String url)
{
if (contains(url, "fail")) return ReadError.BAD_READ!;
if (contains(url, "fail")) return ReadError.BAD_READ?;
if (contains(url, "head-missing")) return { .head = null };
if (contains(url, "title-missing")) return { @dupe(Head { .title = null }) };
if (contains(url, "title-empty")) return { @dupe(Head { .title = @dupe((VarString)null) }) };
VarString str;
if (contains(url, "title-empty")) return { @dupe(Head { .title = @dupe((DString)null) }) };
DString str;
str.printf("Title of %s", url);
return { @dupe(Head { .title = @dupe(str) }) };
}
@@ -96,9 +96,9 @@ fault TitleResult
fn bool! isTitleNonEmpty(Doc doc)
{
if (!doc.head) return TitleResult.TITLE_MISSING!;
VarString* head = doc.head.title;
if (!head) return TitleResult.TITLE_MISSING!;
if (!doc.head) return TitleResult.TITLE_MISSING?;
DString* head = doc.head.title;
if (!head) return TitleResult.TITLE_MISSING?;
return head.len() > 0;
}
@@ -133,7 +133,7 @@ fn void main()
bool! has_title = readWhetherTitleNonEmpty(url);
// This looks a bit less than elegant, but as you see it's mostly due to having to
// use printf here.
io::printf(" Has title: %s vs %s\n", bool_to_string(has_title) ?? (catch? has_title).nameof, has_title ?? false);
io::printf(" Has title: %s vs %s\n", bool_to_string(has_title) ?? (@catchof(has_title)).nameof, has_title ?? false);
};
dynamic_arena.reset();
}

View File

@@ -0,0 +1,52 @@
import std::io;
struct Resource
{
String name;
}
fault Error
{
WELCOME_TO_YOUR_DOOM
}
fn Resource! resource_init(String name)
{
io::printfn("open %s", name);
return { name };
}
fn void Resource.deinit(Resource this) => io::printfn("close %s", this.name);
macro void! @open_with(String name; @body(Resource resource))
{
Resource resource = resource_init(name)!;
defer {
io::printn("Using open_with to close");
resource.deinit();
}
@body(resource);
}
fn Resource! prep_out(String out_name, String[] prep_names)
{
Resource writer = resource_init(out_name)!; // Rethrow the optional result
defer catch writer.deinit();
foreach (name : prep_names)
{
@open_with(name; Resource reader)
{
io::printfn("use %s", reader.name);
// if (true) return Error.WELCOME_TO_YOUR_DOOM?;
}!;
}
return writer;
}
fn void! main()
{
Resource writer = prep_out("out", String[] { "a", "b"})!;
defer writer.deinit();
io::printn("use out");
}

View File

@@ -23,10 +23,10 @@ int err_count = 0;
fn int! askGuess(int high)
{
libc::printf("Guess a number between 1 and %d: ", high);
String text = readLine()?;
String text = readLine()!;
char* end = null;
int value = (int)libc::strtol(text.ptr, &end, 10);
if (end && end[0] >= ' ') return InputResult.NOT_AN_INT!;
if (end && end[0] >= ' ') return InputResult.NOT_AN_INT?;
return value;
}
@@ -34,7 +34,7 @@ fn String! readLine()
{
char* chars = tmalloc(1024);
isz loaded = getline(&chars, &&(usz)1023, libc::stdin());
if (loaded < 0) return InputResult.FAILED_TO_READ!;
if (loaded < 0) return InputResult.FAILED_TO_READ?;
chars[loaded] = 0;
return (String)chars[0..(loaded - 1)];
}
@@ -44,7 +44,7 @@ fn int! askGuessMulti(int high)
while (true)
{
int! result = askGuess(high);
if (catch? result == InputResult.NOT_AN_INT)
if (@catchof(result) == InputResult.NOT_AN_INT)
{
libc::printf("I didn't understand that.\n");
err_count++;
@@ -59,7 +59,7 @@ fn void! Game.play(Game *game)
{
while (!game.done)
{
int guess = askGuessMulti(game.high)?;
int guess = askGuessMulti(game.high)!;
game.report(guess);
game.update(guess);
}

View File

@@ -2,7 +2,7 @@ module load_world;
import std::io;
fn void! main()
{
File f = file::open("examples/hello_world.txt", "rb")?;
File f = file::open("examples/hello_world.txt", "rb")!;
defer f.close()!!;
while (!f.eof())
{

View File

@@ -35,17 +35,17 @@ fn void Map.init(Map *map, uint capacity = 128)
fn Type! Map.valueForKey(Map *map, Key key)
{
if (!map.map) return MapResult.KEY_NOT_FOUND!;
if (!map.map) return MapResult.KEY_NOT_FOUND?;
uint hash = key.hash();
usz pos = hash & map.mod;
Entry* entry = &map.map[pos];
if (!entry) return MapResult.KEY_NOT_FOUND!;
if (!entry) return MapResult.KEY_NOT_FOUND?;
while (entry)
{
if (entry.hash == hash && entry.key == key) return entry.value;
entry = entry.next;
}
return MapResult.KEY_NOT_FOUND!;
return MapResult.KEY_NOT_FOUND?;
}
fn Type! Map.set(Map *map, Key key, Type value) @maydiscard
@@ -67,7 +67,7 @@ fn Type! Map.set(Map *map, Key key, Type value) @maydiscard
entry.value = value;
entry.hash = hash;
entry.key = key;
return MapResult.KEY_NOT_FOUND!;
return MapResult.KEY_NOT_FOUND?;
}
if (entry.hash == hash && entry.key == key)
{
@@ -87,7 +87,7 @@ fn Type! Map.set(Map *map, Key key, Type value) @maydiscard
new.next = null;
new.used = true;
entry.next = new;
return MapResult.KEY_NOT_FOUND!;
return MapResult.KEY_NOT_FOUND?;
}
}

View File

@@ -144,7 +144,7 @@ fn void scale_bodies(Planet[] bodies, double scale)
fn void main(String[] args)
{
int n = args.len < 2 ? 50000000 : str::to_int(args[1])!!;
int n = args.len < 2 ? 50000000 : args[1].to_int()!!;
Planet[] bodies = &planet_bodies;
offset_momentum(bodies);

View File

@@ -13,9 +13,9 @@ fault TokenResult
fn void main(String[] args)
{
// Grab a string from stdin
VarString s = io::stdin().getline();
DString s = io::stdin().getline();
// Delete it at scope end [defer]
defer s.destroy();
defer s.free();
// Grab the string as a slice.
String numbers = s.str();
@@ -77,8 +77,8 @@ fn String! read_next(String* remaining)
}
// If it's a zero length token, return an optional result.
if (!len) return TokenResult.NO_MORE_TOKENS!;
if (!len) return TokenResult.NO_MORE_TOKENS?;
// Otherwise create a slice from the pointer start and length.
return ptr_start[:len];
return (String)ptr_start[:len];
}

View File

@@ -9,14 +9,14 @@ fault TestErr
fn int! eventually_succeed()
{
static int i = 0;
if (i++ < 3) return TestErr.NOPE!;
if (i++ < 3) return TestErr.NOPE?;
return i * 3;
}
macro @retry(#function, int retries = 3)
{
var $Type = $typeof(#function);
anyerr e;
anyfault e;
do
{
$Type! result = #function;

View File

@@ -43,7 +43,7 @@ fn void eval_AtA_times_u(double[] u, double[] atau, double[] x)
fn void main(String[] args)
{
int n = args.len == 2 ? str::to_int(args[1])!! : 2000;
int n = args.len == 2 ? args[1].to_int()!! : 2000;
temparr = malloc(double, n);
double[] u = malloc(double, n);
double[] v = malloc(double, n);

View File

@@ -0,0 +1,17 @@
import std::io;
import std::time;
import std::math;
fn void main()
{
Clock start = clock::now();
DateTime d = datetime::now();
io::printfn("Today is: %d-%02d-%02d %02d:%02d", d.year, d.month.ordinal + 1, d.day, d.hour, d.min);
io::printfn("Epoch timestamp: %d", d.time / 1_000);
TzDateTime td = d.to_local();
int absolute_offset = math::abs(td.gmt_offset);
int offset_hour = absolute_offset / 3600;
int offset_min = (absolute_offset / 60) % 60;
io::printfn("Local time is: %d-%02d-%02d %02d:%02d:%02d %c%02d:%02d", td.year, td.month.ordinal + 1, td.day, td.hour, td.min, td.sec, td.gmt_offset < 0 ? '-' : '+', offset_hour, offset_min);
io::printfn("Executed the above in %d ns", start.to_now());
}

View File

@@ -0,0 +1,7 @@
c3yacc: grammar.y c3.l
bison -d grammar.y; cc -c grammar.tab.c
flex c3.l; gcc -c lex.yy.c
cc -o c3yacc lex.yy.o grammar.tab.o
clean:
rm -f c3yacc grammar.output *.o grammar.tab.* lex.yy.c

View File

@@ -1,98 +1,196 @@
D [0-9]
DU [0-9_]
UN [_]
L [a-zA-Z_]
AN [a-zA-Z_0-9]
H [a-fA-F0-9]
HU [a-fA-F0-9_]
UA [A-Z_0-9]
O [0-7]
B [0-1]
DC [a-z]
UC [A-Z]
CONST [_]*{UC}{UA}*
TYPE [_]*{UC}{UA}*{DC}{AN}*
IDENTIFIER [_]*{DC}{AN}*
E [Ee][+-]?{D}+
P [Pp][+-]?{D}+
B64 [ \t\v\n\f]?[A-Za-z0-9+/][ \t\v\n\fA-Za-z0-9+/=]+
HEX [ \t\v\n\f]?[A-Fa-f0-9][ \t\v\n\fA-Fa-f0-9]+
INTTYPE [ui](8|16|32|64|128)?
REALTYPE [f](8|16|32|64|128)?
INT {D}(_*{D})*
HINT {H}(_*{H})*
OINT {O}(_*{O})*
BINT {B}(_*{B})*
%x COMMENT RAW_STRING
%{
#include <stdio.h>
#include "y.tab.h"
#include "grammar.tab.h"
void count(void);
void comment(void);
int comment_level = 0;
%}
%%
"/*" { comment(); }
"$alignof" { count(); return(CT_ALIGNOF); }
"$assert" { count(); return(CT_ASSERT); }
"$case" { count(); return(CT_CASE); }
"$checks" { count(); return(CT_CHECKS); }
"$default" { count(); return(CT_DEFAULT); }
"$defined" { count(); return(CT_DEFINED); }
"$echo" { count(); return(CT_ECHO); }
"$else" { count(); return(CT_ELSE); }
"$endfor" { count(); return(CT_ENDFOR); }
"$endforeach" { count(); return(CT_ENDFOREACH); }
"$endif" { count(); return(CT_ENDIF); }
"$endswitch" { count(); return(CT_ENDSWITCH); }
"$eval" { count(); return(CT_EVAL); }
"$evaltype" { count(); return(CT_EVALTYPE); }
"$extnameof" { count(); return(CT_EXTNAMEOF); }
"$for" { count(); return(CT_FOR); }
"$foreach" { count(); return(CT_FOREACH); }
"$if" { count(); return(CT_IF); }
"$nameof" { count(); return(CT_NAMEOF); }
"$offsetof" { count(); return(CT_OFFSETOF); }
"$qnameof" { count(); return(CT_QNAMEOF); }
"$sizeof" { count(); return(CT_SIZEOF); }
"$stringify" { count(); return(CT_STRINGIFY); }
"$switch" { count(); return(CT_SWITCH); }
"$typefrom" { count(); return(CT_TYPEFROM); }
"$typeof" { count(); return(CT_TYPEOF); }
"$vaarg" { count(); return(CT_VAARG); }
"$vaconst" { count(); return(CT_VACONST); }
"$vacount" { count(); return(CT_VACOUNT); }
"$vaexpr" { count(); return(CT_VAEXPR); }
"$varef" { count(); return(CT_VAREF); }
"$vasplat" { count(); return(CT_VASPLAT); }
"$vatype" { count(); return(CT_VATYPE); }
"/*" { count(); BEGIN(COMMENT); }
<COMMENT>{
"/*" { count(); comment_level++; }
"*"+"/" { count(); if (comment_level) { comment_level--; } else { BEGIN(INITIAL); } }
"*"+ { count(); }
[^/*\n]+ { count(); }
[/] { count(); }
\n { count(); }
}
\/\/.* { count(); }
"any" { count(); return(ANY); }
"anyfault" { count(); return(ANYFAULT); }
"asm" { count(); return(ASM); }
"assert" { count(); return(ASSERT); }
"bitstruct" { count(); return(BITSTRUCT); }
"bool" { count(); return(BOOL); }
"break" { count(); return(BREAK); }
"case" { count(); return(CASE); }
"catch" { count(); return(CATCH); }
"char" { count(); return(CHAR); }
"const" { count(); return(CONST); }
"continue" { count(); return(CONTINUE); }
"default" { count(); return(DEFAULT); }
"defer" { count(); return(DEFER); }
"define" { count(); return(DEFINE); }
"distinct" { count(); return(DISTINCT); }
"do" { count(); return(DO); }
"double" { count(); return(DOUBLE); }
"else" { count(); return(ELSE); }
"enum" { count(); return(ENUM); }
"extern" { count(); return(EXTERN); }
"false" { count(); return(FALSE); }
"fault" { count(); return(FAULT); }
"finalize" { count(); return(FINALIZE); }
"float" { count(); return(FLOAT); }
"bfloat16" { count(); return(BFLOAT16); }
"float16" { count(); return(FLOAT16); }
"float128" { count(); return(FLOAT128); }
"fn" { count(); return(FN); }
"for" { count(); return(FOR); }
"foreach" { count(); return(FOREACH); }
"foreach_r" { count(); return(FOREACH_R); }
"fn" { count(); return(FUNC); }
"ichar" { count(); return(ICHAR); }
"if" { count(); return(IF); }
"import" { count(); return(IMPORT); }
"initialize" { count(); return(INITIALIZE); }
"inline" { count(); return(INLINE); }
"int" { count(); return(INT); }
"uint" { count(); return(UINT); }
"int128" { count(); return(INT128); }
"iptr" { count(); return(IPTR); }
"isz" { count(); return(ISZ); }
"long" { count(); return(LONG); }
"macro" { count(); return(MACRO); }
"module" { count(); return(MODULE); }
"nextcase" { count(); return(NEXTCASE); }
"ulong" { count(); return(ULONG); }
"null" { count(); return(NUL); }
"return" { count(); return(RETURN); }
"short" { count(); return(SHORT); }
"ushort" { count(); return(USHORT); }
"fault" { count(); return(ERROR); }
"module" { count(); return(MODULE); }
"struct" { count(); return(STRUCT); }
"static" { count(); return(STATIC); }
"switch" { count(); return(SWITCH); }
"tlocal" { count(); return(TLOCAL); }
"true" { count(); return(TRUE); }
"try" { count(); return(TRY); }
"typedef" { count(); return(TYPEDEF); }
"typeid" { count(); return(TYPEID); }
"uint" { count(); return(UINT); }
"uint128" { count(); return(UINT128); }
"ulong" { count(); return(ULONG); }
"union" { count(); return(UNION); }
"uptr" { count(); return(UPTR); }
"ushort" { count(); return(USHORT); }
"usz" { count(); return(USZ); }
"var" { count(); return(VAR); }
"void" { count(); return(VOID); }
"while" { count(); return(WHILE); }
"null" { count(); return(NUL); }
"$for" { count(); return(CTFOR); }
"$foreach" { count(); return(CTFOREACH); }
"$case" { count(); return(CTCASE); }
"$switch" { count(); return(CTSWITCH); }
"$default" { count(); return(CTDEFAULT); }
"$if" { count(); return(CTIF); }
"$else" { count(); return(CTELSE); }
"$endif" { count(); return(CTENDIF); }
"$endswitch" { count(); return(CTENDIF); }
"$endforeach" { count(); return(CTENDFOREACH); }
"$endfor" { count(); return(CTENDFOR); }
@[_]*[A-Z]{UA}* { count(); return(AT_CONST_IDENT); }
#[_]*[A-Z]{UA}* { count(); return(HASH_CONST_IDENT); }
$[_]*[A-Z]{UA}* { count(); return(CT_CONST_IDENT); }
[_]*[A-Z]{UA}* { count(); return(CONST_IDENT); }
@[_]*[A-Z]{UA}*[a-z]{AN}* { count(); return(AT_TYPE_IDENT); }
#[_]*[A-Z]{UA}*[a-z]{AN}* { count(); return(HASH_TYPE_IDENT); }
$[_]*[A-Z]{UA}*[a-z]{AN}* { count(); return(CT_TYPE_IDENT); }
[_]*[A-Z]{UA}*[a-z]{AN}* { count(); return(TYPE_IDENT); }
@[_]*[a-z]{AN}* { count(); return(AT_IDENT); }
#[_]*[a-z]{AN}* { count(); return(HASH_IDENT); }
$[_]*[a-z]{AN}* { count(); return(CT_IDENT); }
[_]*[a-z]{AN}* { count(); return(IDENT); }
0[xX]{H}+ { count(); return(CONSTANT); }
0{D}+? { count(); return(CONSTANT); }
{D}+ { count(); return(CONSTANT); }
L?'(\\.|[^\\'])+' { count(); return(CONSTANT); }
@{CONST} { count(); return(AT_CONST_IDENT); }
#{CONST} { count(); return(HASH_CONST_IDENT); }
${CONST} { count(); return(CT_CONST_IDENT); }
{CONST} { count(); return(CONST_IDENT); }
@{TYPE} { count(); return(AT_TYPE_IDENT); }
#{TYPE} { count(); return(HASH_TYPE_IDENT); }
${TYPE} { count(); return(CT_TYPE_IDENT); }
{TYPE} { count(); return(TYPE_IDENT); }
@{IDENTIFIER} { count(); return(AT_IDENT); }
#{IDENTIFIER} { count(); return(HASH_IDENT); }
${IDENTIFIER} { count(); return(CT_IDENT); }
{IDENTIFIER} { count(); return(IDENT); }
0[xX]{HINT}{INTTYPE}? { count(); return(INTEGER); }
0[oO]{OINT}{INTTYPE}? { count(); return(INTEGER); }
0[bB]{BINT}{INTTYPE}? { count(); return(INTEGER); }
{INT}{INTTYPE}? { count(); return(INTEGER); }
x\'{HEX}+\' { count(); return(BYTES); }
x\"{HEX}+\" { count(); return(BYTES); }
x\`{HEX}+\` { count(); return(BYTES); }
b64\'{B64}+\' { count(); return(BYTES); }
b64\"{B64}+\" { count(); return(BYTES); }
b64\`{B64}+\` { count(); return(BYTES); }
{D}+{E} { count(); return(CONSTANT); }
{D}*"."{D}+({E})? { count(); return(CONSTANT); }
{D}+"."{D}*({E})? { count(); return(CONSTANT); }
{INT}{E}{REALTYPE}? { count(); return(REAL); }
0[xX]{HINT}{P}{REALTYPE}? { count(); return(REAL); }
{INT}"."{INT}{E}?{REALTYPE}? { count(); return(REAL); }
0[xX]{HINT}"."{HINT}{P}{REALTYPE}? { count(); return(REAL); }
L?\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); }
\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); }
\'(\\.|[^\\'])*\' { count(); return(CHAR_LITERAL); }
"`" { count(); BEGIN(RAW_STRING); }
<RAW_STRING>{
"``" { count(); }
"`" { count(); BEGIN(INITIAL); return(STRING_LITERAL); }
"*"+ { count(); }
<<EOF>> { BEGIN(INITIAL); return(RAW_STRING); }
}
"..." { count(); return(ELLIPSIS); }
".." { count(); return(DOTDOT); }
">>=" { count(); return(RIGHT_ASSIGN); }
"<<=" { count(); return(LEFT_ASSIGN); }
">>=" { count(); return(SHR_ASSIGN); }
"<<=" { count(); return(SHL_ASSIGN); }
"+=" { count(); return(ADD_ASSIGN); }
"-=" { count(); return(SUB_ASSIGN); }
"*=" { count(); return(MUL_ASSIGN); }
@@ -101,8 +199,8 @@ L?\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); }
"&=" { count(); return(AND_ASSIGN); }
"^=" { count(); return(XOR_ASSIGN); }
"|=" { count(); return(OR_ASSIGN); }
">>" { count(); return(RIGHT_OP); }
"<<" { count(); return(LEFT_OP); }
">>" { count(); return(SHR_OP); }
"<<" { count(); return(SHL_OP); }
"++" { count(); return(INC_OP); }
"--" { count(); return(DEC_OP); }
"&&" { count(); return(AND_OP); }
@@ -111,8 +209,13 @@ L?\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); }
">=" { count(); return(GE_OP); }
"==" { count(); return(EQ_OP); }
"!=" { count(); return(NE_OP); }
"??" { count(); return(OPTELSE); }
"::" { count(); return(SCOPE); }
"?:" { count(); return(ELVIS); }
"=>" { count(); return(IMPLIES); }
"[<" { count(); return(LVEC); }
">]" { count(); return(RVEC); }
"$$" { count(); return(BUILTIN); }
";" { count(); return(';'); }
("{") { count(); return('{'); }
("}") { count(); return('}'); }
@@ -120,7 +223,6 @@ L?\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); }
":" { count(); return(':'); }
"=" { count(); return('='); }
"(" { count(); return('('); }
"@" { count(); return(AT); }
")" { count(); return(')'); }
("[") { count(); return('['); }
("]") { count(); return(']'); }
@@ -138,8 +240,8 @@ L?\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); }
"^" { count(); return('^'); }
"|" { count(); return('|'); }
"?" { count(); return('?'); }
"{|" { count(); return(FN_BLOCK_BEGIN); }
"|}" { count(); return(FN_BLOCK_END); }
"{|" { count(); return(LBRAPIPE); }
"|}" { count(); return(RBRAPIPE); }
[ \t\v\n\f] { count(); }
. { /* ignore bad characters */ }
@@ -150,26 +252,6 @@ int yywrap(void)
return 1;
}
void comment(void)
{
char c, c1;
loop:
while ((c = input()) != '*' && c != 0)
putchar(c);
if ((c1 = input()) != '/' && c != 0)
{
unput(c1);
goto loop;
}
if (c != 0)
putchar(c1);
}
int column = 0;
void count(void)

File diff suppressed because it is too large Load Diff

View File

@@ -1,19 +1,20 @@
module bigint;
import libc;
macro max(a, b)
macro @max(a, b)
{
return a > b ? a : b;
}
// Horribly bad implementation of BigInt with add/sub.
public struct BigInt
struct BigInt
{
byte* number;
char* number;
uint length;
char sign;
ichar sign;
}
public fn void BigInt.init(BigInt* bigInt)
fn void BigInt.init(BigInt* bigInt)
{
bigInt.number = malloc(1);
bigInt.number[0] = 0;
@@ -21,9 +22,9 @@ public fn void BigInt.init(BigInt* bigInt)
bigInt.sign = 1;
}
public fn void BigInt.initFromString(BigInt* bigInt, char* str)
fn void BigInt.initFromString(BigInt* bigInt, char* str)
{
uint size = strlen(str);
uint size = (uint)libc::strlen(str);
bigInt.sign = 1;
switch (str[0])
{
@@ -46,7 +47,7 @@ public fn void BigInt.initFromString(BigInt* bigInt, char* str)
bigInt.length = size;
}
public fn void BigInt.copyTo(BigInt* source, BigInt* target)
fn void BigInt.copyTo(BigInt* source, BigInt* target)
{
target.number = realloc(target.number, source.length);
target.sign = source.sign;
@@ -54,7 +55,7 @@ public fn void BigInt.copyTo(BigInt* source, BigInt* target)
for (uint i = 0; i < target.length; i++) target.number[i] = source.number[i];
}
public fn void BigInt.destroy(BigInt* bigInt)
fn void BigInt.destroy(BigInt* bigInt)
{
free(bigInt.number);
}
@@ -62,8 +63,8 @@ public fn void BigInt.destroy(BigInt* bigInt)
fn void BigInt.addIgnoreSign(BigInt* a, BigInt* b, BigInt* result)
{
uint length = @max(a.length, b.length) + 1;
byte* res = malloc(length);
char carry = 0;
char* res = malloc(length);
ichar carry = 0;
BigInt* x;
BigInt* y;
if (a.length > b.length)
@@ -98,7 +99,7 @@ fn void BigInt.addIgnoreSign(BigInt* a, BigInt* b, BigInt* result)
result.length = length;
}
public fn void BigInt.getMaxVal(BigInt* bigInt, uint* pos, int* val)
fn void BigInt.getMaxVal(BigInt* bigInt, uint* pos, int* val)
{
for (uint i = bigInt.length; i > 0; i++)
{
@@ -113,14 +114,14 @@ public fn void BigInt.getMaxVal(BigInt* bigInt, uint* pos, int* val)
*val = 0;
}
public fn char BigInt.compare(BigInt* a, BigInt* b)
fn ichar BigInt.compare(BigInt* a, BigInt* b)
{
if (a.sign != b.sign) return a.sign;
byte aMax;
int aMax;
uint aMaxPos;
a.getMaxVal(&aMaxPos, &aMax);
if (aMaxPos >= b.length) return a.sign;
byte bMax;
int bMax;
uint bMaxPos;
b.getMaxVal(&bMaxPos, &bMax);
if (aMaxPos > bMaxPos) return a.sign;
@@ -130,13 +131,13 @@ public fn char BigInt.compare(BigInt* a, BigInt* b)
return 0;
}
public fn char BigInt.compareNoSign(BigInt* a, BigInt* b)
fn ichar BigInt.compareNoSign(BigInt* a, BigInt* b)
{
byte aMax;
int aMax;
uint aMaxPos;
a.getMaxVal(&aMaxPos, &aMax);
if (aMaxPos >= b.length) return 1;
byte bMax;
int bMax;
uint bMaxPos;
b.getMaxVal(&bMaxPos, &bMax);
if (aMaxPos > bMaxPos) return 1;
@@ -149,7 +150,7 @@ public fn char BigInt.compareNoSign(BigInt* a, BigInt* b)
fn void BigInt.subIgnoreSign(BigInt* a, BigInt* b, BigInt* result)
{
uint length = @max(a.length, b.length);
byte* res = malloc(length);
char* res = malloc(length);
BigInt* x;
BigInt* y;
@@ -166,11 +167,11 @@ fn void BigInt.subIgnoreSign(BigInt* a, BigInt* b, BigInt* result)
y = b;
}
byte borrow = 0;
char borrow = 0;
for (uint i = 0; i < length; i++)
{
byte aValue = i >= x.length ? 0 : x.number[i];
byte bValue = borrow + (i >= y.length ? 0 : y.number[i]);
char aValue = i >= x.length ? 0 : x.number[i];
char bValue = borrow + (i >= y.length ? 0 : y.number[i]);
if (bValue > aValue)
{
borrow = 1;
@@ -187,7 +188,7 @@ fn void BigInt.subIgnoreSign(BigInt* a, BigInt* b, BigInt* result)
result.length = length;
}
public fn void BigInt.add(BigInt* a, BigInt* b, BigInt* result)
fn void BigInt.add(BigInt* a, BigInt* b, BigInt* result)
{
if (a.sign == b.sign)
{
@@ -205,12 +206,12 @@ public fn void BigInt.add(BigInt* a, BigInt* b, BigInt* result)
}
}
public fn char* BigInt.toCharArray(BigInt* bigInt)
fn char* BigInt.toCharArray(BigInt* bigInt)
{
uint charLen = bigInt.length + 1 + (bigInt.sign < 0 ? 1 : 0);
byte* out = malloc(charLen);
out[charLen - 1] = '\0';
byte* start = out;
uint icharLen = bigInt.length + 1 + (bigInt.sign < 0 ? 1 : 0);
char* out = malloc(icharLen);
out[icharLen - 1] = '\0';
char* start = out;
if (bigInt.sign < 0)
{
out[0] = '-';
@@ -219,7 +220,7 @@ public fn char* BigInt.toCharArray(BigInt* bigInt)
bool nonZeroFound = false;
for (uint i = bigInt.length; i > 0; i--)
{
byte digit = bigInt.number[i - 1];
char digit = bigInt.number[i - 1];
if (i > 1 && !nonZeroFound && digit == 0) continue;
nonZeroFound = true;
*(start++) = digit + '0';
@@ -227,24 +228,24 @@ public fn char* BigInt.toCharArray(BigInt* bigInt)
return out;
}
public fn void BigInt.print(BigInt* bigInt)
fn void BigInt.print(BigInt* bigInt)
{
char* chars = bigInt.toCharArray();
puts(chars);
libc::puts(chars);
free(chars);
}
/*
public fn void BigInt.fprint(BigInt* bigInt, FILE* file)
fn void BigInt.fprint(BigInt* bigInt, FILE* file)
{
char* chars = bigInt.toCharArray();
fputs(chars, file);
ichar* ichars = bigInt.toicharArray();
fputs(ichars, file);
putc('\n', file);
free(chars);
free(ichars);
}*/
public fn int main(int size, char** args)
fn int main(int size, char** args)
{
BigInt minus2;
BigInt minus1;
@@ -257,7 +258,7 @@ public fn int main(int size, char** args)
for (int i = 1; i <= 500; i++)
{
minus1.add(&minus2, &current);
printf("%d : ", i);
libc::printf("%d : ", i);
current.print();
minus1.copyTo(&minus2);
current.copyTo(&minus1);

View File

@@ -12,9 +12,9 @@ fn void readpgm(char* name, Pixmap* p)
pnm_readpaminit(fp, &inpam);
p.x = inpam.width;
p.y = inpam.height;
if (!(p.p = malloc(@safe(p.x + p.y)))
if (!(p.p = malloc(@safe(p.x + p.y))))
{
@F1("Error at malloc");
@f1("Error at malloc");
}
for (int i = 0; i < inpam.height; i++)
{
@@ -24,7 +24,7 @@ fn void readpgm(char* name, Pixmap* p)
{
p.p[@nowrap(i * inpam.width + j)] = sample;
}
}
};
}
}

View File

@@ -32,8 +32,8 @@ extern fn void printf(char *s);
fn void test_signedunsigned()
{
char a = 0 - 1;
byte b = (byte)(a);
ichar a = 0 - 1;
char b = (char)(a);
printf("Signed-unsigned -1 0xFF \n");
if (a > b) printf("a > b\n");

View File

@@ -1,272 +0,0 @@
module bigint;
macro @max(a, b)
{
return a > b ? a : b;
}
// Horribly bad implementation of BigInt with add/sub.
public struct BigInt
{
byte& number;
uint length;
char sign;
}
public fn void BigInt.init(BigInt& bigInt)
{
bigInt.number = malloc(1);
bigInt.number[0] = 0;
bigInt.length = 1;
bigInt.sign = 1;
}
public fn void BigInt.initFromString(BigInt& bigInt, char& str)
{
uint size = strlen(str);
bigInt.sign = 1;
switch (str[0])
{
case '-':
bigInt.sign = -1;
size--;
str++;
case '+':
size--;
str++;
default:
break;
}
char* res = malloc(size);
for (uint i = 0; i < size; i++)
{
res[i] = str[size - i - 1] - '0';
}
bigInt.number = res;
bigInt.length = size;
}
public fn BigInt& BigInt.newFromString(char& str)
{
BigInt& bigInt = malloc(sizeof(BigInt));
bigInt.initFromString(str);
return bigInt;
}
public fn void BigInt.copyTo(BigInt& source, BigInt& target)
{
target.number = realloc(target.number, source.length);
target.sign = source.sign;
target.length = source.length;
for (uint i = 0; i < target.length; i++) target.number[i] = source.number[i];
}
public fn void BigInt.destroy(BigInt& bigInt)
{
free(bigInt.number);
}
fn void BigInt.addIgnoreSign(BigInt& a, BigInt& b, BigInt& result)
{
uint length = @max(a.length, b.length) + 1;
byte* res = malloc(length);
char carry = 0;
BigInt* x;
BigInt* y;
if (a.length > b.length)
{
x = a;
y = b;
}
else
{
x = b;
y = a;
}
for (uint i = 0; i < length; i++)
{
if (i >= y.length)
{
res[i] = carry + (i >= x.length ? 0 : x.number[i]);
}
else
{
res[i] = x.number[i] + y.number[i] + carry;
}
carry = 0;
if (res[i] > 9)
{
carry = 1;
res[i] -= 10;
}
}
result.destroy();
result.number = res;
result.length = length;
}
public fn void BigInt.getMaxVal(BigInt &bigInt, uint& pos, int& val)
{
for (uint i = bigInt.length; i > 0; i++)
{
if (bigInt.number[i] != 0)
{
*pos = i;
*val = bigInt.number[i];
return;
}
}
*pos = 0;
*val = 0;
}
public fn char BigInt.compare(BigInt& a, BigInt& b)
{
if (a.sign != b.sign) return a.sign;
byte aMax;
uint aMaxPos;
a.getMaxVal(&aMaxPos, &aMax);
if (aMaxPos >= b.length) return a.sign;
byte bMax;
uint bMaxPos;
b.getMaxVal(&bMaxPos, &bMax);
if (aMaxPos > bMaxPos) return a.sign;
if (aMaxPos < bMaxPos) return -a.sign;
if (aMax > bMax) return a.sign;
if (aMax < bMax) return -a.sign;
return 0;
}
public fn char BigInt.compareNoSign(BigInt& a, BigInt& b)
{
byte aMax;
uint aMaxPos;
a.getMaxVal(&aMaxPos, &aMax);
if (aMaxPos >= b.length) return 1;
byte bMax;
uint bMaxPos;
b.getMaxVal(&bMaxPos, &bMax);
if (aMaxPos > bMaxPos) return 1;
if (aMaxPos < bMaxPos) return -1;
if (aMax > bMax) return 1;
if (aMax < bMax) return -1;
return 0;
}
fn void BigInt.subIgnoreSign(BigInt& a, BigInt& b, BigInt& result)
{
uint length = @max(a.length, b.length);
byte& res = malloc(length);
BigInt& x;
BigInt& y;
if (a.compareNoSign(b) < 0)
{
result.sign = -1;
x = b;
y = a;
}
else
{
result.sign = 1;
x = a;
y = b;
}
byte borrow = 0;
for (uint i = 0; i < length; i++)
{
byte aValue = i >= x.length ? 0 : x.number[i];
byte bValue = borrow + (i >= y.length ? 0 : y.number[i]);
if (bValue > aValue)
{
borrow = 1;
aValue += 10;
}
else
{
borrow = 0;
}
res[i] = aValue - bValue;
}
result.destroy();
result.number = res;
result.length = length;
}
public fn void BigInt.add(BigInt& a, BigInt& b, BigInt& result)
{
if (a.sign == b.sign)
{
a.addIgnoreSign(b, result);
result.sign = a.sign;
return;
}
if (a.sign < 0)
{
b.subIgnoreSign(a, result);
}
else
{
a.subIgnoreSign(a, result);
}
}
public fn char* BigInt.toCharArray(BigInt* bigInt)
{
uint charLen = bigInt.length + 1 + (bigInt.sign < 0 ? 1 : 0);
byte* out = malloc(charLen);
out[charLen - 1] = '\0';
byte* start = out;
if (bigInt.sign < 0)
{
out[0] = '-';
start++;
}
bool nonZeroFound = false;
for (uint i = bigInt.length; i > 0; i--)
{
byte digit = bigInt.number[i - 1];
if (i > 1 && !nonZeroFound && digit == 0) continue;
nonZeroFound = true;
*(start++) = digit + '0';
}
return out;
}
public fn void BigInt.print(BigInt& bigInt)
{
char* chars = bigInt.toCharArray();
puts(chars);
free(chars);
}
/*
public fn void BigInt.fprint(BigInt* bigInt, FILE* file)
{
char* chars = bigInt.toCharArray();
fputs(chars, file);
putc('\n', file);
free(chars);
}*/
public fn int main(int size, char*& args)
{
BigInt minus2;
BigInt minus1;
BigInt current;
minus2.initFromString("0");
minus1.initFromString("1");
current.initFromString("0");
for (int i = 1; i <= 500; i++)
{
minus1.add(&minus2, &current);
printf("%d : ", i);
current.print();
minus1.copyTo(&minus2);
current.copyTo(&minus1);
}
return 0;
}

View File

@@ -1,190 +0,0 @@
module foo;
int oefk = 1;
int bob = 'HELO';
typedef Foo* as Bob;
fn void testdefer(int x)
{
defer printf("A");
defer
{
int x = 2;
printf("B");
x = x + 1;
}
//defer catch printf("B")
//defer catch (error e) printf("%s", @name(e));
//if (x = 1) return throw Error.FOO;
printf("!");
}
struct Foo
{
int i;
Bar *x;
}
struct Bar
{
Foo foo;
Foo* fooPtr;
Bob helo;
struct bar
{
int a;
}
}
union Xyz
{
int x;
int y;
}
$if (1)
{
struct Ogg
{
Zed *bob;
}
struct DOgg
{
Zed bob;
}
struct Zed
{
Foo b;
}
}
struct Simple
{
int i;
double j;
}
fn int boo()
{
Zed zfe;
Simple s = { 1 };
s = { 2 };
//Simple x = { j = 1 };
//Zed zfed = { 1 };
int eok = 0;
{
eok = 2;
}
eok = eok * 2 + 1 * 2;
eok = eok / 2 + 32 / 2;
eok = eok % 2 - 32 % 45;
int de = eok;
de++;
de = de << 1;
de = de >> 1;
de = de ^ (byte)(1);
de = de & 1;
de = de | 1;
if (de > 100 || de < 10 || de <= 1000 || de >= 1921 || de == 100 || de != 2)
{
return boo();
}
switch (eok)
{
case 1
eok++;
next;
case 3:
eok++;
next;
eok--;
case 2:
eok--;
default:
eok--;
break;
}
if (eok > 0)
{
eok++;
}
else
{
eok--;
}
int xfeef = -eok;
/*
bool xoek = !eok;
int oekfefo = ~eok;
int *booe = &eok;
int **de = &booe;
int &xfok = &eok;
int &&bo = &xfok;
int e12 = **bo;
int fe = *(&eok);
int x2 = *xfok;*/
int x1 = 2;
FOO:
x1 = x1 + 1;
goto FOO;
bool z = 123 > 3.0;
{
int x = 0;
}
{
int x = 1;
}
int j = 10;
do
{
j = j + 10;
} while (j > 2);
return 1;
}
fn int helo(int i)
{
return i + 1;
}
fn void while_test()
{
int a = 10;
while (int b = 37; a < 0)
{
a++;
int xy = 1;
}
}
fn void test()
{
int a = 10;
while (1)
{
int xy = 1;
}
int eokfe = boo();
int i = -1;
bool dwf = !2.0;
ushort b = ~(byte)(0);
int j as k;
int l as m = 0;
int o = 0, p = 3;
short f = (char)((int)(-2));
//int doek = ~2;
return;
}
fn void main()
{
helo(2);
printf("Helo\n");
}

View File

@@ -13,7 +13,7 @@ struct Foo
fn Foo createFoo(int a, int b = 10)
{
// Compound initializer
return Foo({a, b});
return Foo {a, b};
}
struct Bar
@@ -37,8 +37,7 @@ fn void testStruct()
foo = createFoo(.b = 13, .a = 14);
printf("Foo a:%d b:%d\n", foo.a, foo.b);
// Structural copy
Bar bar = (Bar)(foo);
Bar bar = bitcast(foo, Bar);
printf("Bar x:%d y:%d\n", bar.x, bar.y);
// Type functions

View File

@@ -1,18 +0,0 @@
module baz;
import bar;
struct Foo
{
int x;
int y;
}
//define bar::blub(Foo, 1) as fooblub;
public fn void main()
{
Foo f = { 3, 4 };
//Foo g = fooblub(&f);
}

View File

@@ -1,7 +0,0 @@
module bar(Type, i);
public fn Type blub(Type type)
{
type.x = i;
return type;
}

View File

@@ -1,24 +0,0 @@
module foo;
const int = 0;
struct Boo
{
int i;
union
{
int death;
};
}
fn void test()
{
int i = 0;
i++;
if (i < 100)
{
int j = 0;
j += i;
return;
}
}

View File

@@ -1,30 +0,0 @@
module foo;
extern fn void printf(char *hello, ...);
error MyError;
error YourError;
fn 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");
}
}

View File

@@ -1,19 +1,19 @@
extern fn void printf(char* message, ...);
public fn void defer1() {}
public fn void defer2() {}
public fn void defer3() {}
public fn void defer4() {}
public fn void defer5() {}
public fn void defer6() {}
public fn void defer7() {}
public fn void defer8() {}
public fn void defer9() {}
public fn void defer10() {}
public fn void defer11() {}
fn void defer1() {}
fn void defer2() {}
fn void defer3() {}
fn void defer4() {}
fn void defer5() {}
fn void defer6() {}
fn void defer7() {}
fn void defer8() {}
fn void defer9() {}
fn void defer10() {}
fn void defer11() {}
public fn int main(int argc)
fn int main(int argc)
{
int a = 0;
{