Skip to content
Open

bug #64

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ecd7ed4
add cross-product and exponent to core numbers library
Jul 11, 2016
d5d63b2
Merge pull request #53 from HuoLanguage/ast_revamp
caocaojiudao Jul 12, 2016
27c5546
Merge branch 'master' of https://github.com/HuoLanguage/huo
Jul 12, 2016
8dd88ce
Fix bug #54
TheLoneWolfling Jul 13, 2016
1cb5e93
Merge branch 'master' of https://github.com/HuoLanguage/huo
Jul 13, 2016
e9e2a7c
add typeof command @TheLoneWolfling
Jul 17, 2016
6a004ef
add path_utils file
Jul 17, 2016
e24aef1
various bug fixes
Aug 4, 2016
d7f637e
Fix switch function, arguments were being arranged incorrectly for ea…
caocaojiudao Apr 11, 2017
999bbec
Update README.md
caocaojiudao Apr 12, 2017
c866e71
Update README.md
caocaojiudao Apr 13, 2017
152af3b
add first class functions
caocaojiudao Apr 12, 2017
c697bbc
Corrected ordering of value.h and huo_ast typedef
snordgren Jun 10, 2017
6e38ea3
Added execption to backtracking for Windows machines
snordgren Jun 10, 2017
f9a670c
Added Cygwin to the list of defines used to identify Windows OS
snordgren Jun 10, 2017
a654fa0
Fixed %llu being used to print unsigned long
snordgren Jun 10, 2017
0fc931a
Renamed Value_type enum values to have TYPE_ prefix in order to preve…
snordgren Jun 10, 2017
f023157
Moved the include of base_util.h to after the include of Windows.h to…
snordgren Jun 10, 2017
6aeb577
Temporarily removed -Werror compiler directive
snordgren Jun 10, 2017
cc7a9ce
Added .exe files to .gitignore
snordgren Jun 10, 2017
28533cf
Added TYPE_ prefix to Value_type uses
snordgren Jun 10, 2017
099c86f
Added carriage return character to whitespace characters
snordgren Jun 10, 2017
dee5f2e
Added support for backslash as file separator in path_utils get_path_dir
snordgren Jun 10, 2017
3e24d0f
Added removal of .mk files in project folder to Makefile clean
snordgren Jun 11, 2017
8640bb3
Added .vscode directory to gitignore
snordgren Jun 11, 2017
1fb8bd8
Added .exe.stackdump files to .gitignore
snordgren Jun 11, 2017
d18b908
Merge pull request #59 from snordgren/windows
caocaojiudao Aug 27, 2017
6639414
Update README.MD
caocaojiudao Sep 7, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ huo
*.o
*.mk
*~
*.exe
.vscode/
*.exe.stackdump
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CFLAGS += -g3
LIBS = -lpthread

# Be super strict about everything
CFLAGS += -std=c11 -Werror -Wall -Wextra -pedantic -O2
CFLAGS += -std=c11 -Wall -Wextra -pedantic -O2
CPPFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2

# Automatically sort out header dependencies
Expand Down Expand Up @@ -50,14 +50,15 @@ objs = \
src/parser.o \
src/execute.o \
src/config.o \
src/path_utils.o \
src/huo.o

all: huo

huo: $(objs)
$(CC) $(LDFLAGS) -o huo $(objs) $(LIBS)

clean: ; rm -f -- ./src/*.mk ./src/*.o & rm -f ./src/execution_functions/*.o & rm -f ./src/structures/*.o
clean: ; rm -f -- ./src/*.mk ./src/*.o & rm -f ./src/execution_functions/*.o & rm -f ./src/structures/*.o & rm -f ./.*.mk

.PHONY: all clean
.DELETE_ON_ERROR:
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Huo is an interpreted language which I started as a hobby project. The original

If you would like to contribute to Huo open an issue describing the work you would like to do. If the change is accepted then you can make a pull request with your changes and it will be merged in after review.

##compile
## compile
```shell
make
```

##run
## run
Create a file containing Huo code and run it with the interpreter:
```shell
$ ./huo test.huo
Expand All @@ -18,7 +20,7 @@ Or you can run the included REPL which is written in Huo. To exit the REPL simpl
"Hello, world!"
```

##Functions
## Functions

basic math
```lisp
Expand Down Expand Up @@ -115,6 +117,21 @@ if you want to simply return a composition of values, use the return function
(let x (pair 0 "start"))
(print x) ; [ 0, "start" ]
```
ast and run together allow you to pass functions as arguments
the keywork ast returns its first argument as a value of type ast
the keyword run will execute an ast value with whatever arguments you pass in
```lisp
(def mapper arr fnc
(do
(each arr item i
(set i (run fnc item) arr)
)
(return arr)
)
)

(msg_first "Hello" (ast (cat x " World")))
```
switch block
```lisp
; the switch block is convenient for matching a value against a large number
Expand Down Expand Up @@ -194,17 +211,6 @@ in order, returning the value from the last function inside it
)
)
```
a parallel block takes any number of functions and executes them
in parallel. The parallel block returns undefined. Beware of passing
the same variable to two different functions in a parallel block!
```lisp
(let x 0)
(let y [])
(parallel
(for 0 100 (let x (+ x 1)))
(for 0 100 (set (length y) (length y) y))
)
```
reading a file is simple and returns a string
```lisp
(let file (read "./files/example.txt"))
Expand Down
50 changes: 50 additions & 0 deletions core/numbers.huo
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,53 @@
(return result)
)
)

(def exponent x n
(switch n
(= 0 1)
(= 1 x)
(< 1 (do
(let result x)
(for 1 n (do
(let result (* result x))
)
)
(return result)
)
)
(> 0 (do
(let result 1)
(for 0 n (let result (/ result x)))
(return result)
)
)
)
)

(def cross_product x y
(do
(let result [])
(each x xitem xidx
(do
(set xidx [] result)
(each y yitem yidx
(do
(let sum 0)
(each (index xidx x) kitem kidx
(let sum
(+ sum
(*
(index kidx (index xidx x))
(index yidx (index kidx y))
)
)
)
)
(set yidx sum (index xidx result))
)
)
)
)
(return result)
)
)
19 changes: 19 additions & 0 deletions examples/first-class.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(def msg_first msg fnc
(do
(print msg)
(print (run fnc msg))
)
)

(def mapper arr fnc
(do
(each arr item i
(set i (run fnc item) arr)
)
(return arr)
)
)

(msg_first "Hello" (ast (cat x " World")))

(print (mapper [1, 2, 3] (ast (+ item 3))))
9 changes: 7 additions & 2 deletions examples/while.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
(let x [0])
(let y 1)
(while
(< (length x) 10000)
(push 0 x)
(< (length x) 100000)
(do
(let y (+ 1 y))
(push y x)
)

)
(print x)
(print (length x))
2 changes: 1 addition & 1 deletion src/apply_core_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Value apply_core_function(struct Value *kwd_val, struct Execution_bundle
}
else if(string_matches_heap(&kwd, &split_const)){
v = split(*a, *b);
} else if (a->type == ARRAY && b->type == ARRAY) {
} else if (a->type == TYPE_ARRAY && b->type == TYPE_ARRAY) {
struct Value_array *a_arr = value_as_array(a);
struct Value_array *b_arr = value_as_array(b);
if(a_arr->size != b_arr->size){
Expand Down
20 changes: 20 additions & 0 deletions src/apply_execution_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,25 @@ bool apply_execution_function(struct Value *kwd_val, struct Value *result, struc
*result = value_from_undef();
return true;
}
else if(string_matches_heap(&kwd, &run_const)){
exec_bundle->ast = ast_child(ast, 1);
struct Value fn_value = execute(exec_bundle);
huo_ast *fn = value_as_ast(&fn_value);
size_t index = 1;
for(size_t i = 2; i < ast_size(ast); i++){
ast_set_child(fn, index, ast_child(ast, i));
index++;
}

exec_bundle->ast = fn;
*result = execute(exec_bundle);
exec_bundle->ast = ast;
return true;
}
else if(string_matches_heap(&kwd, &ast_const)){

*result = value_from_ast(ast_copy(ast_child(ast, 1)));
return true;
}
return false;
}
6 changes: 4 additions & 2 deletions src/apply_single_value_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Value apply_single_value_func(struct Value *kwd_val, struct Execution_bun
if(string_matches_heap(&kwd, &print_const)){
print(*value);
printf("\n");
value->type = UNDEF;
value->type = TYPE_UNDEF;
value->data.bl = false;
} else if(string_matches_heap(&kwd, &length_const)){
*value = value_from_long(length(*value));
Expand All @@ -34,8 +34,10 @@ struct Value apply_single_value_func(struct Value *kwd_val, struct Execution_bun
exec_bundle->ast = read_import(value_as_string(value));
*value = execute(exec_bundle);
exec_bundle->ast = old_ast;
} else if(string_concat_heap(&kwd, &typeof_const)){
*value = value_from_string(type_to_string(value->type));
} else {
value->type = UNDEF;
value->type = TYPE_UNDEF;
value->data.bl = false;
}
return *value;
Expand Down
6 changes: 3 additions & 3 deletions src/base_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool __size_t_mul_overflow(size_t a, size_t b, size_t *res) {
return false;
} else {
WARN_ONCE("size_t is HOW long?");
WARN_ONCE("Using slow but portable overflow test")
WARN_ONCE("Using slow but portable overflow test");
}
#else
#pragma message ("Using slow but portable overflow test")
Expand Down Expand Up @@ -85,11 +85,11 @@ struct Value sub_vars(struct Value *v, struct Scopes *scopes, huo_depth_t max_de
if (max_depth <= 0) {
ERROR("Max depth exceeded in computation");
}
if (v->type == ARRAY) {
if (v->type == TYPE_ARRAY) {
for (size_t i = 0; i < v->data.array->size; i++) {
*(v->data.array->values[i]) = sub_vars(v->data.array->values[i], scopes, max_depth);
}
} else if (v->type == KEYWORD) {
} else if (v->type == TYPE_KEYWORD) {
struct Value *w = NULL;
struct String kwd = value_as_keyword(v);
if ((w = get_letted_value(scopes, kwd)) != NULL) {
Expand Down
12 changes: 12 additions & 0 deletions src/base_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <stdio.h>
#include <stdbool.h>
#include "structures/structures.h"
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__CYGWIN__)
#include <execinfo.h>
#endif
#include <assert.h>
#include "config.h"

Expand Down Expand Up @@ -39,6 +41,7 @@

/* Macro because it makes printf errors easier to detect at compile time */
#define ERROR(...) ERROR_AT(__FILE__, __func__, __LINE__, __VA_ARGS__)
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__CYGWIN__)
#define ERROR_AT(FILE, FUNC, LINE, ...) do {\
void * buffer[5];\
char ** strings;\
Expand All @@ -53,6 +56,15 @@
/*assert(false);*/\
exit(1);\
} while (0);
#else
#define ERROR_AT(FILE, FUNC, LINE, ...) do {\
fprintf(stderr, "Error at %s:%s:%i: ", FILE, FUNC, LINE);\
fprintf(stderr, __VA_ARGS__);\
fprintf(stderr, "\n");\
/*assert(false);*/\
exit(1);\
} while (0);
#endif

#define ARR_MALLOC(num_elem, elem_val) malloc_or_die(arr_malloc_size((num_elem), sizeof(elem_val)))

Expand Down
13 changes: 13 additions & 0 deletions src/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ struct String print_const = STR_NEW("print");

struct String concat_const = STR_NEW("cat");

struct String run_const = STR_NEW("run");

struct String ast_const = STR_NEW("ast");

struct String if_const = STR_NEW("if");

struct String def_const = STR_NEW("def");
Expand Down Expand Up @@ -66,8 +70,17 @@ struct String false_const = STR_NEW("false");

struct String true_const = STR_NEW("true");

struct String typeof_const = STR_NEW("typeof");

struct String function_names = STR_NEW("[if, read, let, set, each, for, do, switch, parallel, import, map, reduce, substring, while]");

struct String keyword_const = STR_NEW("keyword");
struct String number_const = STR_NEW("number");
struct String string_const = STR_NEW("string");
struct String boolean_const = STR_NEW("boolean");
struct String array_const = STR_NEW("array");
struct String undefined_const = STR_NEW("undefined");

const char open_parens_const = '(';
const char close_parens_const = ')';
const char root_type_const = 'r';
Expand Down
10 changes: 10 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ extern struct String read_line_const;
extern struct String function_names;
extern struct String false_const;
extern struct String true_const;
extern struct String typeof_const;
extern struct String run_const;
extern struct String ast_const;

extern struct String keyword_const;
extern struct String number_const;
extern struct String string_const;
extern struct String boolean_const;
extern struct String array_const;
extern struct String undefined_const;

extern const char open_parens_const;
extern const char close_parens_const;
Expand Down
Loading