1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00
Commit Graph

2269 Commits

Author SHA1 Message Date
Ben Dudson
ef2d054491 Fix bug in concat, add test to step 7
Concatenating multiple empty lists resulted in a return
which should be empty but was not checked.

(concat (list) (list))
-> ()

Test added to step 7 to catch, since this appeared in
self-hosting step 8 in the -> macro expansion.
2017-12-16 23:52:59 +00:00
Ben Dudson
ccd081039e Fix list and repl output
* (list) wasn't always incrementing reference counts when needed

* The value from eval of a file shouldn't be printed, so defined
  a small routine read_eval which doesn't print the final value.

Self-hosting now runs to step8, where two tests of the -> macro fail.
2017-12-15 23:58:46 +00:00
Ben Dudson
dfefe35bd9 Add time-ms function
Uses clock_gettime (syscall 228) to get time in seconds and
nanoseconds, then converts to ms.
2017-12-15 23:11:20 +00:00
Ben Dudson
524c684990 Fix bug in rest and println
* rest wasn't incrementing reference count of CAR pointer
  since cl was overwritten by a write to RCX

* println wasn't putting a space separator between items

MAL self-hosting now runs to step 6. Fails since (time-ms) not
defined.
2017-12-14 23:06:19 +00:00
Ben Dudson
6b47dc878f Fix bug in concat. MAL step 2 now passes
Wasn't incrementing reference count when copying content
of a vector single argument.
2017-12-13 23:45:50 +00:00
Ben Dudson
a22fcc1b0d Fix small bug in comparison
Comparing maps clobbered the block type, needed
when comparing array types
2017-12-13 00:07:47 +00:00
Ben Dudson
0119b269f2 Adding gensym, clean or macro
Replacement for earlier macro, now using gensym
2017-12-12 23:50:34 +00:00
Ben Dudson
1f254872a3 Add meta, with-meta and ^ reader
Native and user-defined functions now have metadata,
which can be set using with-data, and retrieved using meta.

The ^ reader converts "^ A B" into (with-meta B A)
2017-12-12 23:49:07 +00:00
Ben Dudson
4f9baa2bc4 Start step A, add readline
prints header string, readline working.
2017-12-11 23:21:48 +00:00
Ben Dudson
13b2edea4c Add vals and dissoc functions
step 9 now only 3 soft-fail tests. All comparisons of maps,
where the order of keys shouldn't matter but does.
2017-12-11 22:39:42 +00:00
Ben Dudson
39ad52ec27 Add assoc function, small fixes
Step 9 now 3 failing, 9 soft fail

missing vals and dissoc functions.
2017-12-09 19:19:13 +00:00
Ben Dudson
a4e26fd0e6 Add try*/catch*
20 failing tests for step 9. Mainly assoc not yet implemented
2017-12-09 16:14:12 +00:00
Ben Dudson
ff31757288 Add keyword and keyword? functions 2017-12-09 11:52:50 +00:00
Ben Dudson
7511108d0e Add get, sequential?, symbol, vector and hash-map functions
hash-map currently doesn't check that the number of arguments
is even, but just marks the list as a map.
2017-12-09 11:05:53 +00:00
Ben Dudson
b584079d06 Fix swap! function
Changes to how eval works mean the previous
approach no longer works. Now uses the same method as
apply and map functions, calling apply_fn for user-defined
functions.
2017-12-08 21:26:30 +00:00
Ben Dudson
4a65988988 Add apply function to core
Tested both built-in and user functions, seems
to be working
2017-12-08 20:49:02 +00:00
Ben Dudson
a4ca0426a2 Adding map and throw functions
Also some more useful error messages for the arithmetic
operators, rather than returning nil.
2017-12-08 08:19:57 +00:00
Ben Dudson
2c686f42ea Fix bug in apply_fn releasing memory
The list was released too early, before the function body
had its reference count incremented. If the function was
a lambda function then this released the function, leading
to odd behaviour

e.g. ((fn* [x] (+ 1 x)) 2)
2017-12-08 08:17:35 +00:00
Ben Dudson
c71ed43f49 Add vector? map? and contains?
vector? and map? almost the same as list? so just
change list? to take a container type argument.
2017-12-03 23:20:37 +00:00
Ben Dudson
ad66dd9d7d Adding predicate functions to core
nil? true? false? number?  test the contents type

symbol? string? fn? and macro? test object types, similar to the atom?
function

These two sets of functions are each implemented as one function with
different input parameters.
2017-12-01 22:35:19 +00:00
Ben Dudson
7410ca9e12 Fix bug in read_str for long strings
Symbols are read by the tokenizer into RSI, but RSI
was overwritten when a new string chunk was fetched.
If the reader happened to be reading a symbol as the
end of an Array chunk was reached, then the symbol
would be changed to part of the input string.

All step 8 tests now pass.
2017-11-30 23:46:03 +00:00
Ben Dudson
bd1961f76b rest core function always returns list
If given a vector, allocates a new Cons for the head
of the list, pointing to the rest of the elements.
This is still O(1), since only needs to copy one element.
2017-11-30 23:41:32 +00:00
Ben Dudson
d00896dcad step 8 macros
* Added macroexpand functions and special symbol

* Some changes to how AST is freed by apply_fn,
  to avoid having to shuffle the stack after a call.

7 failing tests. "rest" can return vector or list,
but should always return a list. Other errors in
-> and ->> macros seem more difficult to solve.
2017-11-30 00:20:25 +00:00
Ben Dudson
6df6dfd358 Fix memory leak, eval releases AST
Quasiquote (and macroexpand) replace the AST in eval.
This makes releasing the memory harder, so could give
rise to both double-frees, and memory leaks.

Eval now releases both its environment (for tail calls)
and the AST.
2017-11-29 15:13:10 +00:00
Ben Dudson
07ac7a2714 Adding nth and rest functions
nth returns an element at given index in a list.

rest returns a list or vector with the first element removed.
Currently this works by just returning a pointer to the second element.
2017-11-28 16:16:53 +00:00
Ben Dudson
f2030f8696 Fix raw_to_string function for long strings
Previously assumed that the string would fit into a single Array
object. Now handles the allocation of a chain of Array objects.
2017-11-28 15:11:43 +00:00
Ben Dudson
08caa2faa0 Adding core first function
Returns the first element of a list or vector
2017-11-28 15:03:13 +00:00
Ben Dudson
5ad295bd9d Fix cons and quasiquote functions
* Better handling of the final element in quasiquote

* Cons now increments reference counts of objects pointed to
  so they are not accidentally deleted

Now passes step7 tests again, including new test
2017-11-27 15:51:15 +00:00
Ben Dudson
4a99a16c41 Adding test to step7 for quasiquote
Handling of the last element of the AST
is tested by having an unquote as the last element.
2017-11-27 14:07:00 +00:00
Ben Dudson
181dd159a3 All step 7 tests pass
concat sets the container type so that return is always a list.

NOTE: The container type of every Cons is not changed, so if the
list is sliced then the pieces may be vectors or lists.
2017-11-24 22:51:39 +00:00
Ben Dudson
8d054892e1 quasiquote tests pass
Needed to call quasiquote with first AST element,
which then wrapped symbol in (quote )

Two tests still failing. One for cons with empty list,
and another due to concat returning a vector
2017-11-23 00:06:34 +00:00
Ben Dudson
6b0641cbab Working on quasiquote. Not yet done
Quoting not yet working properly, so symbols in the input
AST are evaluated when they should not be.

Example failing test:

(quasiquote (1 a 3))

The quasiquote function turns this into

(cons 1 (cons a (quote (3))))

rather than

(cons 1 (cons (quote a) (quote (3))))
2017-11-22 23:29:46 +00:00
Ben Dudson
12ab92c379 Fix bugs in string manipulation
Handling corner cases when string chunks
(Arrays) are full. This caused segfaults
if string lengths hit magic numbers.
2017-11-21 12:00:25 +00:00
Ben Dudson
a69523e790 Add cons, concat functions
* Helper function cons_seq_copy, which copies a list or vector
  This is used in concat to copy prepended lists (final list not
  copied).

* Bug fix in printer, which would segfault if the container
  of a Cons did not match that of the first Cons e.g. a vector in a
  list.

* Seems to be a bug in print, which now segfaults on printing *env*
2017-11-20 23:46:19 +00:00
Ben Dudson
5fc8a017d9 Add quote special form
Just returns its argument unmodified, but some
differences in handling values and pointers needed.

Added generic Makefile rule, builds step7_quote
2017-11-18 23:12:38 +00:00
Ben Dudson
4459835a1c Fix bug in string_append_string
When copying from a long string, the wrong length was
used for the source string. This caused junk to get into
the destination string.
2017-11-18 14:34:02 +00:00
Ben Dudson
0a5a3ab339 Adding *ARGV* command-line arguments
Can now run a script passed as first argument,
and puts any remaining arguments into a list *ARGV*.

All step 6 tests now pass

Some missing/buggy things:

* The error handler should be different to the REPL one,
  or go into the REPL correctly on error

* Strange errors occur if the file is too long. Suspect string
  problems
2017-11-17 23:30:43 +00:00
Ben Dudson
43092cf0e1 Fix bug in swap! for complex types
Need to increment the reference count or the object
will be garbage collected after evaluation.
2017-11-17 22:08:59 +00:00
Ben Dudson
1f819490b6 swap! function tests pass
Added a core function swap! It works, but has some warts:

* No error message is thrown on error yet, needs adding
* To run the function, swap! calls into the middle of eval.
  This probably indicates that eval needs to be split up.
2017-11-16 22:56:21 +00:00
Ben Dudson
ea550eba37 Fixed bug in str and eval, add string_copy
* str was not copying the first argument, which was then being appended to.
  If the str call was inside a function, then a string literal in the function
  body would be modified with every call, leading to strange behaviour.

* eval was crashing on a value input like (eval nil) because
  the return was not put into rax as it should.

Added string_copy, used in str function to copy the first input
so that it can then be modified.

Most step 6 tests now pass. 10 failing due to missing *ARGV* and swap!
2017-11-14 22:09:56 +00:00
Ben Dudson
be99f655c1 Improved handling of comments, whitespace
Previously a comment would end input. Now looks for
a line feed and resumes parsing.
2017-11-14 21:08:43 +00:00
Ben Dudson
1b30ad76a8 Adding support for atoms
* Functions atom, atom?, deref, reset!

* Printer support for atoms

* Type maltype_atom
2017-11-12 23:46:18 +00:00
Ben Dudson
193bb92016 Added eval and load-file functions
Had to add code to reader to ignore
LF and CR as whitespace.
2017-11-12 08:01:19 +00:00
Ben Dudson
cd34fb3b2e Added read-string and slurp functions
System calls using Linux 64-bit syscalls
in system.asm. Wrapper code in core.asm
2017-11-11 23:33:58 +00:00
Ben Dudson
cc38947443 Bugfix in let and def forms
Was releasing the environment too many times,
so it got deleted/reused when it shouldn't.

Step 5 tests now pass.
2017-11-11 08:05:27 +00:00
Ben Dudson
2edb2d3cf2 Step 5: Tail call optimisation
Not working yet, still get memory-related errors
2017-11-11 01:04:11 +00:00
Ben Dudson
b43cd5b639 Fixes for long strings
Extra code in string_append_char, string_append_string,
and print_string to handle long strings.

Printing *env* no longer produces segfault.
2017-11-09 23:25:37 +00:00
Ben Dudson
17d45192b9 print_readably flag, str and println functions
Additional flag to pr_str in RDI. If zero, returns
string without escaping special characters.

Added str and println functions, using most of the same
code as pr-str and prn

Only one test now failing for step 4.
2017-11-09 00:13:20 +00:00
Ben Dudson
2eba19559b step 4 keywords, []/() comparison, variadic args
Finished support for "& more" style args. Now handles case
where "more" is empty.

Better handling of empty list, removing use of static empty list,
since this seemed to lead to hard-to-debug memory issues.

Comparison between Cons container types (vector, list, map for now)
ignores container type, so (= [] (list)) -> true

Step 4 down to 19 failing tests, all due to missing str and println
functions.
2017-11-08 23:31:09 +00:00
Ben Dudson
aa722d73db step 4 non-deferrable tests passing
Down to 37 failing tests. Some odd ones, maybe indicating
something strange going on with memory for string arrays.
2017-11-07 23:30:11 +00:00