1 2 3 4 5 | var float f = 1.23; var int i = 66; var [20]char buf; /* array of 20 chars allocated on the stack */ var char* str = "Hello World!"; var char* str = new(char, 20); /* array of 20 chars allocated on the heap */ |
1 2 3 4 | proc int add(int a, int b)
{
return a + b;
}
|
1 2 3 4 5 6 | include "str.hoc";
proc void hello_world()
{
print_str("Hello World!");
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | include "str.hoc";
/*
For all integers from 1 to 'n' print:
fizz if it's divisible by 3
buzz if it's divisible by 5
fizzbuzz if it's divisible by 15
else print the integer itself
*/
proc void fizzbuzz(int n)
{
if(n > 0)
{
var int i = 1;
while(i <= n)
{
if((i % 15) == 0)
print_str("fizzbuzz");
else if((i % 3) == 0)
print_str("buzz");
else if((i % 5) == 0)
print_str("fizz");
else
print_int(i);
print_str("\n");
i = i + 1;
}
}
}
proc int main()
{
fizzbuzz(100);
return 0;
}
|
1 | cmd$ hocc.exe my_program.hoc |
1 2 | > bin\hocc.exe hoc\test.hoc w:\hoc\hocc.c(298) : could not read file `vm.exe` |
could not read file `vm.exe`Fixed; the problem is that the compiler needs the `vm.exe` file which is expected to be located in the working directory. That file is an implementation of a stack-machine and the compiler is generating code for it - the output executable `test.exe` is in fact a copy of `vm.exe` plus the code.
The float printing could be better: printing 1.0 instead of 0.1E1Done.
..the compiler didn't warn about using uninitialized variables. Are there default values ?In this version the compiler does not do data flow analysis at all.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | typedef struct AstBlock
{
AstNode;
List node_list;
int block_id;
int nesting_depth;
AstBlock* encl_block;
List local_decls;
List nonlocal_occurs;
List access_links;
int links_size;
int locals_size;
}
AstBlock;
...
AstBlock*
new_block(SourceLocation* src_loc)
{
AstBlock* node = mem_push_struct(arena, AstBlock);
node->kind = AstNodeKind_Block;
...
|
1 2 3 4 5 6 7 | typedef struct Foo
{
struct // this is anon struct
{
int x, y;
};
} Foo;
|
1 2 3 4 5 6 7 8 9 | tyepdef struct v2
{
int x, y;
} v2;
typedef struct Foo
{
v2;
} Foo;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | typedef struct
{
AstNode* ast;
}
AstNodeRef;
typedef struct
{
AstNode;
AstNodeRef type_expr;
}
AstPointer;
void DEBUG_print_ast_node(String* str, int indent_level, AstNode* node, char* tag);
...
{
AstPointer* ptr = (AstPointer*)node;
DEBUG_print_ast_node(str, indent_level, ptr->type_expr, "type_expr");
}
|
1 2 3 4 5 6 7 8 9 10 11 12 | typedef struct a { int x; } a; typedef struct a2 { int x; } a2[2]; struct b { a; }; struct b2 { a2; }; #include <stdio.h> int main() { printf("%zu\n", sizeof(struct b)); printf("%zu\n", sizeof(struct b2)); } |
1 | DEBUG_print_ast_node(str, indent_level, ptr->type_expr, "type_expr"); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | typedef struct AstBlock
{
AstNode;
List node_list;
...
}
AstBlock*
new_block(SourceLocation* src_loc)
{
AstBlock* node = mem_push_struct(arena, AstBlock);
node->kind = AstNodeKind_Block;
...
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | typedef struct AstBlock
{
AstNode node;
List node_list;
...
}
AstBlock*
new_block(SourceLocation* src_loc)
{
AstBlock* node = mem_push_struct(arena, AstBlock);
node->node.kind = AstNodeKind_Block;
...
}
|
1 | 'a2 [2]' : no members defined using this type |
1 2 3 4 5 6 7 8 9 10 11 12 | typedef struct a { int x; } a; typedef struct a2 { int x; } a2[2]; struct b { a; }; struct b2 { struct a2; }; #include <stdio.h> int main() { printf("%d\n", sizeof(struct b)); printf("%d\n", sizeof(struct b2)); } |
1 2 | 4 4 |
..with what syntax do you access "x" variable from b2 structure in second array element?I don't know..
1 2 3 4 5 6 7 8 9 10 11 | proc void fibo(int,n) {}
proc void fibo() { if(true) ; {} }
proc void fibo(int n) { var int*f_n = n; }
proc void fibo() { if(true) { { } } }
proc void fibo() { { fibo(); } }
proc int gcd_(int a, int0b);
proc int*gcd_(int a) { return a; }
proc int gcd_(int a, int b) { & a = b; return a; }
proc int gcd_(int a) { a : a; }
proc int gcd(int );
proc void swap(int* data) { data[0](= 0; }
|
1 2 3 | proc void fibo(int,n) {}
proc int gcd_(int a, int0b);
proc int gcd(int );
|
1 2 3 | proc void fibo() { if(true) ; {} }
proc void fibo() { if(true) { { } } }
proc void fibo() { { fibo(); } }
|
1 | proc int gcd_(int a) { a : a; }
|
1 | proc void swap(int* data) { data[0](= 0; }
|
1 2 | proc int*gcd_(int a) { return a; }
proc int gcd_(int a, int b) { & a = b; return a; }
|
1 2 3 | proc void abs(int a) { a =--a; }
proc void fpdec(int* significand) { significand[ ] = 0; }
proc void print_str(char* str) { putc(*str)+str = str + 1; }
|