Skip to content

Commit db3ca29

Browse files
small tweaks
1 parent aaa8a36 commit db3ca29

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

examples/fib.cog

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ Def Fib
22
Case (< 3) then (1 Drop)
33
else (Let N ; Fib of - 1 N ; + Fib of - 2 N);
44

5-
~~For each in Range 1 to 40 ( Print Fib );
6-
75
Put "The 50th fibonacci number is... ";
86
Print the Fib of 50;
97

src/cognac.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "prelude.h"
55
#include <limits.h>
66
#include <assert.h>
7+
#include <time.h>
78
#include <string.h>
89
#include <unistd.h>
910
#include <ctype.h>
@@ -14,22 +15,22 @@
1415
#include <sys/wait.h>
1516
#include <execinfo.h>
1617

17-
#define STRING_(x) #x
18-
#define STRING(x) STRING_(x)
19-
2018
ast_list_t* full_ast = NULL;
2119
module_t* pmod = NULL;
2220
char* heap = NULL;
2321
module_t prelude1 = { .prefix = "prelude" }; // written in C
2422
module_t prelude2 = { .prefix = "prelude" }; // written in Cognate
2523
module_list_t preludes = { .mod=&prelude2, .next = &(module_list_t){.mod=&prelude1, .next=NULL} };
2624

25+
int usleep (__useconds_t);
26+
char* strdup (const char*);
27+
2728
static bool is_prelude(module_t* mod)
2829
{
2930
return mod == &prelude1 || mod == &prelude2;
3031
}
3132

32-
static void print_banner()
33+
static void print_banner(void)
3334
{
3435
char* banner =
3536
"\t ______ ______\n"
@@ -38,11 +39,11 @@ static void print_banner()
3839
"\t/ /___/ /_/ / /_/ / / / / /_/ / /___\n"
3940
"\t\\____/\\____/\\__, /_/ /_/\\__,_/\\____/\n"
4041
"\t /____/\n"
41-
"\t Cognate Compiler";
42+
"\t Cognate Compiler\n";
4243
puts(banner);
4344
}
4445

45-
_Noreturn static void unreachable()
46+
_Noreturn static void unreachable(void)
4647
{
4748
char msg[] = "\n\n\033[31;1m"
4849
"\t ___ _\n"
@@ -73,7 +74,7 @@ static void* alloc(size_t n)
7374
return (heap += n) - n;
7475
}
7576

76-
where_t* parse_pos()
77+
where_t* parse_pos(void)
7778
{
7879
where_t* p = alloc(sizeof *p);
7980
p->mod = pmod;
@@ -92,8 +93,6 @@ _Noreturn void type_error(val_type_t expected, val_type_t got, where_t* pos)
9293

9394
_Noreturn void throw_error(char* message, where_t* where)
9495
{
95-
// TODO this segfaults if the error is in prelude
96-
9796
/*
9897
puts(message);
9998
puts(where->mod->prefix);
@@ -116,9 +115,10 @@ _Noreturn void throw_error(char* message, where_t* where)
116115
char c;
117116
do { c = fgetc(where->mod->file); } while (c != '\n'); // read one line. TODO this is bad
118117
}
119-
size_t n = SIZE_MAX;
120-
char* line = NULL;
121-
getline(&line, &n, where->mod->file);
118+
119+
char line_start[MAX_ERROR_LINE_LENGTH];
120+
char* line = line_start;
121+
fgets(line, MAX_ERROR_LINE_LENGTH, where->mod->file);
122122

123123
// Now we strip leading whitespace
124124
while (isspace(*line)) line++, offset--;
@@ -231,7 +231,7 @@ func_list_t* push_func(func_t* f, func_list_t* next)
231231
return n;
232232
}
233233

234-
char* make_func_name()
234+
char* make_func_name(void)
235235
{
236236
static size_t fid = 0;
237237
char* str = alloc(20);
@@ -371,7 +371,7 @@ reg_t* pop_register_rear(reg_dequeue_t* registers)
371371
return reg;
372372
}
373373

374-
reg_dequeue_t* make_register_dequeue()
374+
reg_dequeue_t* make_register_dequeue(void)
375375
{
376376
reg_dequeue_t* r = alloc(sizeof *r);
377377
r->front = r->rear = NULL;
@@ -775,10 +775,20 @@ void to_exe(module_t* mod)
775775
};
776776
pid_t p = fork();
777777
if (!p) execvp(args[0], args);
778-
printf("\n%s ", mod->path);
779778
int status;
780-
while (!waitpid(p, &status, WNOHANG)) usleep(10000), fputc('>', stdout), fflush(stdout);
781-
printf(" %s\n", exe_path);
779+
int i = 0;
780+
while (!waitpid(p, &status, WNOHANG))
781+
{
782+
usleep(50000);
783+
fputc('\r', stdout);
784+
printf("%s ", mod->path);
785+
char* bar[3] = { "> >> >> >> >> >> >> >> >> >> ", ">> >> >> >> >> >> >> >> >> >>", " >> >> >> >> >> >> >> >> >> >" };
786+
fputs(bar[i], stdout);
787+
if (++i == 3) i = 0;
788+
printf(" %s", exe_path);
789+
fflush(stdout);
790+
}
791+
fputc('\n', stdout);
782792
if (status != EXIT_SUCCESS) exit(status);
783793
}
784794

@@ -2767,7 +2777,7 @@ void static_calls(module_t* m)
27672777
}
27682778
}
27692779

2770-
ast_list_t* make_astlist()
2780+
ast_list_t* make_astlist(void)
27712781
{
27722782
ast_list_t* a = alloc(sizeof *a);
27732783
ast_list_t* b = alloc(sizeof *b);
@@ -2856,8 +2866,8 @@ void static_branches(module_t* m)
28562866
if (f->stack) clear_registers(regs);
28572867
if (f->returns)
28582868
push_register_front(make_register(f->rettype, a), regs);
2859-
break;
28602869
}
2870+
break;
28612871
case bind:
28622872
{
28632873
reg_t* r = pop_register_front(regs);
@@ -3217,7 +3227,7 @@ lit_t* mk_lit(val_type_t t, const char* str)
32173227
return l;
32183228
}
32193229

3220-
word_list_t* builtins()
3230+
word_list_t* builtins(void)
32213231
{
32223232
static builtin_t b[] =
32233233
{

src/cognac.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include <stdio.h>
44
#include <stdbool.h>
55

6+
#define STRING_(x) #x
7+
#define STRING(x) STRING_(x)
8+
#define MAX_ERROR_LINE_LENGTH 256
9+
610
typedef struct _ptr_assoc_t ptr_assoc_t;
711
typedef struct _func_t func_t;
812
typedef struct _module_t module_t;
@@ -256,15 +260,15 @@ void module_parse(module_t*);
256260
module_t* create_module(char*);
257261
void fold_defs(module_t*);
258262
lit_t* mk_lit(val_type_t, const char*);
259-
word_list_t* builtins();
263+
word_list_t* builtins(void);
260264
const char* c_val_type(val_type_t);
261265
const char* print_val_type(val_type_t);
262266
_Noreturn void throw_error(char*, where_t*);
263267
void load_preludes(void);
264268

265269
extern FILE* yyin;
266270
extern ast_list_t* full_ast;
267-
extern where_t* parse_pos();
271+
extern where_t* parse_pos(void);
268272
int yylex(void);
269273
int yyparse (void);
270274
void yyerror(char*);

src/prelude.cog

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Def Any? ( True Drop );
88
Def Twin as (Let X ; X X);
99
Def Triplet as (Twin ; Twin);
1010

11-
1211
Def When as (
1312
Let Cond;
1413
Let Block;
@@ -83,7 +82,7 @@ Def Prints ( Print Fold ( Join Show ) from "" over Reverse List);
8382
Def Filter (
8483
Def Predicate;
8584
Let L;
86-
Empty;
85+
Empty;
8786
For each in L (
8887
Let I;
8988
Let R be Predicate I;

0 commit comments

Comments
 (0)