Skip to content

Commit 72cf2b2

Browse files
committed
Type check alpha 2, fixes everywhere
1 parent 73c29d8 commit 72cf2b2

12 files changed

+220
-68
lines changed

display.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,23 @@ void pmgn(const char* msg, ...){
5353
printf(ANSI_COLOR_RESET);
5454
}
5555

56+
#ifdef DEBUG
5657
void dbg2(const char* msg, ...){
58+
#else
59+
void dbg(const char* msg, ...){
60+
#endif
5761
printf(ANSI_FONT_BOLD);
5862
printf(ANSI_COLOR_GREEN "\n[Debug] ");
5963
printf(ANSI_COLOR_RESET);
6064
va_list args;
6165
va_start(args, msg);
6266
vprintf(msg, args);
6367
}
64-
68+
#ifdef DEBUG
69+
void info2(const char* msg, ...){
70+
#else
6571
void info(const char* msg, ...){
72+
#endif
6673
printf(ANSI_FONT_BOLD);
6774
printf(ANSI_COLOR_BLUE "\n[Info] ");
6875
printf(ANSI_COLOR_RESET);
@@ -71,7 +78,11 @@ void info(const char* msg, ...){
7178
vprintf(msg, args);
7279
}
7380

81+
#ifdef DEBUG
82+
void err2(const char* msg, ...){
83+
#else
7484
void err(const char* msg, ...){
85+
#endif
7586
printf(ANSI_FONT_BOLD);
7687
printf(ANSI_COLOR_RED "\n[Error] ");
7788
printf(ANSI_COLOR_RESET);
@@ -80,7 +91,11 @@ void err(const char* msg, ...){
8091
vprintf(msg,args);
8192
}
8293

94+
#ifdef DEBUG
95+
void warn2(const char* msg, ...){
96+
#else
8397
void warn(const char* msg, ...){
98+
#endif
8499
printf(ANSI_FONT_BOLD);
85100
printf(ANSI_COLOR_YELLOW "\n[Warning] ");
86101
printf(ANSI_COLOR_RESET);

display.h

+13
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,24 @@ void pylw(const char *msg, ...);
1818
void pcyn(const char *msg, ...);
1919
void pmgn(const char *msg, ...);
2020

21+
#ifdef DEBUG
2122
#define dbg(x, ...) dbg2( ANSI_FONT_BOLD "<%s:%d> " ANSI_COLOR_RESET x, __FILE__, __LINE__, ##__VA_ARGS__)
2223
void dbg2(const char *msg, ...);
24+
25+
#define err(x, ...) err2( ANSI_FONT_BOLD "<%s:%d> " ANSI_COLOR_RESET x, __FILE__, __LINE__, ##__VA_ARGS__)
26+
void err2(const char *msg, ...);
27+
28+
#define info(x, ...) info2( ANSI_FONT_BOLD "<%s:%d> " ANSI_COLOR_RESET x, __FILE__, __LINE__, ##__VA_ARGS__)
29+
void info2(const char *msg, ...);
30+
31+
#define warn(x, ...) warn2( ANSI_FONT_BOLD "<%s:%d> " ANSI_COLOR_RESET x, __FILE__, __LINE__, ##__VA_ARGS__)
32+
void warn2(const char *msg, ...);
33+
#else
34+
void dbg2(const char *msg, ...);
2335
void err(const char *msg, ...);
2436
void info(const char *msg, ...);
2537
void warn(const char *msg, ...);
38+
#endif
2639

2740
void lnerr(const char *msg, Token t, ...);
2841
void lnwarn(const char *msg, Token t, ...);

expr.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ void expr_dispose2(Expression *exp){
6767
void expr_dispose(){
6868
for(uint64_t i = 0;i < cache_pointer;i++){
6969
// if(cache[i] != NULL)
70-
if(cache[i]->type == EXPR_CALL){
70+
if(cache[i]->type == EXPR_CALL || cache[i]->type == EXPR_DEFINE){
7171
free(cache[i]->calex.args);
7272
}
73-
free(cache[i]);
73+
else if(cache[i]->type == EXPR_CONSTANT && cache[i]->consex.token.type == TOKEN_string){
74+
free(cache[i]->consex.sval);
75+
}
76+
free(cache[i]);
7477
}
7578
free(cache);
7679
}

lexer.c

+17-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#include "display.h"
1010

1111

12-
/* The code below is for testing and debugging purposes.
13-
* =====================================================
14-
*/
1512

1613
#ifdef DEBUG
1714

@@ -21,13 +18,23 @@ static const char* tokenStrings[] = {
2118
#undef ET
2219
};
2320

21+
#endif
22+
2423
void lexer_print_token(Token t, uint8_t printType){
2524
for(uint64_t i = 0;i < t.length;i++)
2625
pmgn("%c", t.string[i]);
26+
#ifdef DEBUG
2727
if(printType)
2828
pblue("(%s) \t", tokenStrings[t.type]);
29+
#endif
2930
}
3031

32+
/* The code below is for testing and debugging purposes.
33+
* =====================================================
34+
*/
35+
36+
#ifdef DEBUG
37+
3138
void lexer_print_tokens(TokenList list){
3239
size_t prevLine = 0;
3340
for(uint64_t i = 0;i < list.count;i++){
@@ -249,6 +256,8 @@ static Token nextToken(uint8_t atStart){
249256
return makeToken(unknown);
250257
}
251258

259+
static uint64_t hasErrors = 0;
260+
252261
TokenList tokens_scan(const char* line){
253262
source = strdup(line);
254263
length = strlen(source);
@@ -265,6 +274,7 @@ TokenList tokens_scan(const char* line){
265274
lexer_print_token(t, 0);
266275
printf("'");
267276
token_print_source(list.tokens[list.count - 1], 1);
277+
hasErrors++;
268278
}
269279
}
270280
if(list.tokens[list.count - 1].type != TOKEN_eof)
@@ -277,3 +287,7 @@ void tokens_free(TokenList list){
277287
free(list.tokens);
278288
free(list.source);
279289
}
290+
291+
uint64_t lexer_has_errors(){
292+
return hasErrors;
293+
}

lexer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ typedef struct{
3030
TokenList tokens_scan(const char *source);
3131
void tokens_free(TokenList list);
3232
void token_print_source(Token t, uint8_t reportType);
33+
void lexer_print_token(Token t, uint8_t printType);
34+
uint64_t lexer_has_errors();
35+
3336
//#define DEBUG
3437

3538
#ifdef DEBUG
3639
void lexer_print_tokens(TokenList list);
37-
void lexer_print_token(Token t, uint8_t printType);
3840
#endif

main.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,26 @@ int main(int argc, char *argv[]){
4747
exit(EXIT_FAILURE);
4848
}
4949
TokenList l = tokens_scan(source);
50+
#ifdef DEBUG
5051
lexer_print_tokens(l);
51-
52+
#endif
5253
stmt_init();
5354
expr_init();
5455

5556
BlockStatement s = parse(l);
57+
#ifdef DEBUG
5658
blockstmt_print(s);
59+
#endif
5760
printf("\n");
5861
fflush(stdout);
5962
type_check(s);
6063
printf("\n");
61-
64+
65+
type_dispose();
6266
blockstmt_dispose(s);
6367
tokens_free(l);
6468
free(source);
6569
stmt_dispose();
70+
expr_dispose();
6671
return 0;
6772
}

parser.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static Expression* _primary(){
166166
Expression *ex = expr_new(EXPR_REFERENCE);
167167
ex->refex.token = previousToken;
168168
advance();
169-
ex->refex.refer = expression();
169+
ex->refex.refer = _primary();
170170
switch(ex->refex.refer->type){
171171
case EXPR_REFERENCE:
172172
case EXPR_VARIABLE:
@@ -389,7 +389,6 @@ static Statement* statement_Container(){
389389

390390
static Statement* statement_Return(){
391391
Statement *ret = stmt_new2(RETURN);
392-
ret->rets.token = previousToken;
393392
Expression *ex = expression();
394393
ret->rets.value = ex;
395394
return ret;
@@ -448,9 +447,15 @@ static Statement* statement(){
448447
switch(presentToken.type){
449448
#define KEYWORD(name, a) \
450449
case TOKEN_##name: \
451-
consume2(TOKEN_##name); \
452-
return statement_##name(); \
453-
break;
450+
{ \
451+
Token bak = presentToken; \
452+
consume2(TOKEN_##name); \
453+
Statement *s = statement_##name(); \
454+
if(s != NULL) \
455+
s->token = bak; \
456+
return s; \
457+
break; \
458+
}
454459
#include "keywords.h"
455460
#undef KEYWORD
456461
default:
@@ -477,3 +482,7 @@ BlockStatement parse(TokenList l){
477482
}
478483
return ret;
479484
}
485+
486+
uint64_t parser_has_errors(){
487+
return hasErrors;
488+
}

parser.h

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
#include <stdbool.h>
77

88
BlockStatement parse(TokenList list);
9+
uint64_t parser_has_errors();

stmt.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ typedef struct{
2020
} BlockStatement;
2121

2222
typedef struct IfStatement{
23-
Token token;
2423
Expression *condition;
2524
BlockStatement thenBlock;
2625
Statement *elseIf;
@@ -43,7 +42,6 @@ typedef struct{
4342
} DefineStatement;
4443

4544
typedef struct{
46-
Token token;
4745
Expression *value;
4846
} ReturnStatement;
4947

@@ -70,6 +68,7 @@ typedef enum{
7068

7169
struct Statement{
7270
StatementType type;
71+
Token token;
7372
union{
7473
SetStatement sets;
7574
PrintStatement prints;

tests/typetest.algi

+39
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Container Mine(){
1111
}
1212
Structure a = Mine()
1313
String k = a.mem2.bool
14+
String s2 = a.mem1.bool
15+
Boolean right = a.mem1.bool
16+
Set done = a.mem1.bool
17+
Set another = 5
18+
Set a.mem1.bool = another
1419
Define Function(){
1520
Integer localVar = i
1621
}
@@ -20,6 +25,9 @@ Set q = t.localVar
2025
Container LinkedList(){
2126
Set value = Null
2227
Set next = Null
28+
Container Another(){
29+
Set mem = 0
30+
}
2331
}
2432

2533
Set head = Null
@@ -28,3 +36,34 @@ Define InsertAtBeg(Node){
2836
Set Node.next = head
2937
Set head = Node
3038
}
39+
40+
Define InsertAtEnd(Node){
41+
Set temp = head
42+
While(temp.next != Null){
43+
Set temp = temp.next
44+
}
45+
Set temp.next = Node
46+
}
47+
48+
Define MultipleReturn(){
49+
Integer iValue = 10
50+
String gValue = "Hello World"
51+
If(True){
52+
Return iValue
53+
}
54+
Else{
55+
Return gValue
56+
}
57+
}
58+
59+
Number ret = MultipleReturn()
60+
Set c = "What?"
61+
If(ret < c){
62+
Print "Hoorray!"
63+
}
64+
65+
Define NestedRoutine(){
66+
Define Child(){
67+
String int = 13
68+
}
69+
}

0 commit comments

Comments
 (0)