mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Fixes bug handling chained && ||. Work towards more macro functionality. Correctly emit stdcall. Corrected character parsing in escaped string entries.
This commit is contained in:
@@ -8,6 +8,10 @@ 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)
|
||||
{
|
||||
|
||||
@@ -50,7 +50,7 @@ int findBlockVar(Value th, Value locvars, Value varnm)
|
||||
}
|
||||
|
||||
/* Look for local variable. Returns idx if found, -1 otherwise. */
|
||||
func int CompInfo.findLocalVar(CompInfo *comp, Value varnm)
|
||||
func int CompInfo.findLocalVar(CompInfo *comp, Value varnm) throws SearchError
|
||||
{
|
||||
assert(varnm.isSym());
|
||||
|
||||
@@ -68,7 +68,7 @@ func int CompInfo.findLocalVar(CompInfo *comp, Value varnm)
|
||||
}
|
||||
locvars = arrGet(th, locvars, 0); // link to prior local variables
|
||||
} while (locvars != aNull);
|
||||
return -1;
|
||||
throw SearchError.NOT_FOUND;
|
||||
}
|
||||
|
||||
/* Look for closure variable. Returns idx if found, -1 otherwise. */
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
module game_of_life;
|
||||
|
||||
extern func void printf(char *fmt, ...);
|
||||
extern func int atoi(char *val);
|
||||
|
||||
struct GameBoard
|
||||
{
|
||||
int h;
|
||||
@@ -8,19 +11,27 @@ struct GameBoard
|
||||
byte* temp;
|
||||
}
|
||||
|
||||
extern void *__stdoutp;
|
||||
extern func void fflush(void *std);
|
||||
extern func int rand();
|
||||
extern func void* malloc(usize size);
|
||||
extern func void usleep(int time);
|
||||
|
||||
func void GameBoard.show(GameBoard *board)
|
||||
{
|
||||
printf("\033[H");
|
||||
|
||||
printf("\e[H");
|
||||
byte* current = board.world;
|
||||
for (int y = 0; y < board.h; y++)
|
||||
{
|
||||
for (int x = 0; x < board.w; x++)
|
||||
{
|
||||
printf(*current ? "\033[07m \033[m" : " ");
|
||||
printf(*current ? "\e[07m \e[m" : " ");
|
||||
current++;
|
||||
}
|
||||
printf("\033[E");
|
||||
printf("\e[E");
|
||||
}
|
||||
stdout.fflush();
|
||||
fflush(__stdoutp);
|
||||
}
|
||||
|
||||
func void GameBoard.evolve(GameBoard *board)
|
||||
@@ -34,24 +45,26 @@ func void GameBoard.evolve(GameBoard *board)
|
||||
{
|
||||
for (int x1 = x - 1; x1 <= x + 1; x1++)
|
||||
{
|
||||
int actualX = (x1 + w) % w;
|
||||
int actualY = (y1 + h) % h;
|
||||
if (board.world[x + y * w]) n++;
|
||||
int actualX = (x1 + board.w) % board.w;
|
||||
int actualY = (y1 + board.h) % board.h;
|
||||
if (board.world[actualX + actualY * board.w]) n++;
|
||||
}
|
||||
}
|
||||
if (board.world(x + y * w)) n--;
|
||||
board.temp[x + y * w] = (n == 3 || (n == 2 && board.world(x + y * w)));
|
||||
if (board.world[x + y * board.w]) n--;
|
||||
board.temp[x + y * board.w] = cast(n == 3 || (n == 2 && board.world[x + y * board.w]), byte);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < w * h; i++)
|
||||
for (int i = 0; i < board.w * board.h; i++)
|
||||
{
|
||||
board.world[i] = board.temp[i];
|
||||
}
|
||||
}
|
||||
|
||||
int main(int c, string[] v)
|
||||
|
||||
func int main(int c, char** v)
|
||||
{
|
||||
int w = 0, h = 0;
|
||||
int w = 0;
|
||||
int h = 0;
|
||||
if (c > 1) w = atoi(v[1]);
|
||||
if (c > 2) h = atoi(v[2]);
|
||||
if (w <= 0) w = 30;
|
||||
@@ -60,18 +73,18 @@ int main(int c, string[] v)
|
||||
GameBoard board;
|
||||
board.w = w;
|
||||
board.h = h;
|
||||
board.board = malloc(h * w);
|
||||
board.temp = malloc(h * w);
|
||||
board.world = malloc(cast(h * w, ulong));
|
||||
board.temp = malloc(cast(h * w, ulong));
|
||||
|
||||
for (int i = h * w - 1; i >= 0; i--)
|
||||
{
|
||||
board.world[i] = rand() < RAND_MAX / 10 ? 1 : 0;
|
||||
board.world[i] = rand() % 10 == 0 ? 1 : 0;
|
||||
}
|
||||
while (1)
|
||||
for (int j = 0; j < 1000; j++)
|
||||
{
|
||||
|
||||
board.show();
|
||||
board.evolve();
|
||||
usleep(200000);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -882,6 +882,7 @@ func void testErrors()
|
||||
printf("Throws: %d\n", throwsDone.times);
|
||||
printf("End of errors\n");
|
||||
}
|
||||
|
||||
macro void arrayCallMacro($x)
|
||||
{
|
||||
printf("Array call: %d, %d\n", $x[0], $x[1]);
|
||||
@@ -1089,8 +1090,155 @@ func void testTypeValues()
|
||||
printf("Testunion.a sizeof: %d = 2\n", TestUnionSize.a.sizeof);
|
||||
}
|
||||
|
||||
func int testTypeofHelp()
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
func void testTypeof()
|
||||
{
|
||||
typeid y = typeof(1 + testTypeofHelp());
|
||||
}
|
||||
|
||||
macro void printMessage($x)
|
||||
{
|
||||
for (int i = 0; i < $x; i++)
|
||||
{
|
||||
printf("print message! %d\n", i);
|
||||
}
|
||||
$x = $x + 1;
|
||||
}
|
||||
|
||||
func void testMacros()
|
||||
{
|
||||
int i = 3;
|
||||
@printMessage(i);
|
||||
printf("i: %d\n", i);
|
||||
}
|
||||
|
||||
const char[] LUT_ENC = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
||||
'4', '5', '6', '7', '8', '9', '+', '/',
|
||||
};
|
||||
|
||||
const byte ERR = 0xFF;
|
||||
|
||||
const byte[256] LUT_DEC = {
|
||||
// '+', ',', '-', '.', '/', '0', '1', '2'
|
||||
62, ERR, ERR, ERR, 63, 52, 53, 54,
|
||||
// '3', '4', '5', '6', '7', '8', '9', ':'
|
||||
55, 56, 57, 58, 59, 60, 61, ERR,
|
||||
// ';', '<', '=', '>', '?', '@', 'A', 'B'
|
||||
ERR, ERR, ERR, ERR, ERR, ERR, 0, 1,
|
||||
// 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
|
||||
2, 3, 4, 5, 6, 7, 8, 9,
|
||||
// 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R'
|
||||
10, 11, 12, 13, 14, 15, 16, 17,
|
||||
// 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
|
||||
18, 19, 20, 21, 22, 23, 24, 25,
|
||||
// '[', '\', ']', '^', '_', '`', 'a', 'b'
|
||||
ERR, ERR, ERR, ERR, ERR, ERR, 26, 27,
|
||||
// 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'
|
||||
28, 29, 30, 31, 32, 33, 34, 35,
|
||||
// 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r'
|
||||
36, 37, 38, 39, 40, 41, 42, 43,
|
||||
// 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
|
||||
44, 45, 46, 47, 48, 49, 50, 51,
|
||||
};
|
||||
|
||||
|
||||
const char PAD = '=';
|
||||
const char FIRST = '+';
|
||||
const char LAST = 'z';
|
||||
|
||||
public error Base64Error
|
||||
{
|
||||
INVALID_CHARACTER
|
||||
}
|
||||
|
||||
public func void encode(byte[] in, string *out)
|
||||
{
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; i < in.len; i++);
|
||||
{
|
||||
switch (i % 3)
|
||||
{
|
||||
case 0:
|
||||
out[j++] = LUT_ENC[(in[i] >> 2) & 0x3F];
|
||||
case 1:
|
||||
out[j++] = LUT_ENC[(in[i-1] & 0x3) << 4 + ((in[i] >> 4) & 0xF)];
|
||||
case 2:
|
||||
out[j++] = LUT_ENC[(in[i-1] & 0xF) << 2 + ((in[i] >> 6) & 0x3)];
|
||||
out[j++] = LUT_ENC[in[i] & 0x3F];
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// move back
|
||||
int last = in.len - 1;
|
||||
|
||||
// check the last and add padding
|
||||
switch (last % 3)
|
||||
{
|
||||
case 0:
|
||||
out[j++] = LUT_ENC[(in[last] & 0x3) << 4];
|
||||
out[j++] = PAD;
|
||||
out[j++] = PAD;
|
||||
case 1:
|
||||
out[j++] = LUT_ENC[(in[last] & 0xF) << 2];
|
||||
out[j++] = PAD;
|
||||
}
|
||||
}
|
||||
|
||||
public func int decode(char[] in, byte[] out) throws Base64Error
|
||||
{
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
char value = in[i];
|
||||
|
||||
if (value == PAD) return j;
|
||||
|
||||
if (value < FIRST || in[i] > LAST) throw INVALID_CHARACTER;
|
||||
byte c = LUT_DEC[in[i] - FIRST);
|
||||
if (c == ERR) throw INVALID_CHARACTER;
|
||||
|
||||
switch (i % 4)
|
||||
{
|
||||
case 0:
|
||||
out[j] = c << 2;
|
||||
case 1:
|
||||
out[j++] += (c >> 4) & 0x3;
|
||||
// if not last char with padding
|
||||
if (i < (len - 3) || in[len - 2] != PAD)
|
||||
{
|
||||
out[j] = (c & 0xF) << 4;
|
||||
}
|
||||
case 2:
|
||||
out[j++] += (c >> 2) & 0xF;
|
||||
if (i < (len -2) || in[len -1] != PAD)
|
||||
{
|
||||
out[j] = (c & 0x3) << 6;
|
||||
}
|
||||
case 3:
|
||||
out[j++] += c;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
func int main(int x)
|
||||
{
|
||||
testMacros();
|
||||
testTypeof();
|
||||
testSimple();
|
||||
testErrorBug();
|
||||
testErrors();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module bar;
|
||||
|
||||
import baz::foo;
|
||||
import baz.foo;
|
||||
import testbaz;
|
||||
|
||||
public func void test()
|
||||
|
||||
Reference in New Issue
Block a user