~brenns10/funlisp

5e561eaa3a67190754ac42ab2aafd2d4bc405318 — Stephen Brennan 2 years ago 99a5fa3
Comment support, closes #14
4 files changed, 60 insertions(+), 16 deletions(-)

M CHANGELOG.md
M doc/language.rst
A scripts/comments.lisp
M src/parse.c
M CHANGELOG.md => CHANGELOG.md +2 -0
@@ 10,6 10,8 @@ changes may be made on minor versiosn bumps, and feature additions / bug-fixes
will be made on patch versions.

## [Unreleased]
### Added
- line comments are now supported via the semicolon (`;`) character

## [0.1.0] (BETA) - 2018-06-13


M doc/language.rst => doc/language.rst +13 -0
@@ 29,6 29,19 @@ this script using the ``bin/runfile`` utility bundled with funlisp:
  $ bin/runfile scripts/hello_world.lisp
  hello world

Comments
--------

Line comments are created in funlisp by starting them with a semicolon (``;``).
Everything after that, until the end of the line, is ignored by the parser. Here
are some examples:

.. code:: lisp

  ; i'm a comment
  (print "i'm some code" ; with a comment in the middle
  )

Types
-----


A scripts/comments.lisp => scripts/comments.lisp +15 -0
@@ 0,0 1,15 @@
; I'm a line comment. I'm the only type of comment allowed in funlisp. I can
; occur on a blank line or at the end of a line already containing code.

(+ 5;this is fine
   5)

(define main;this is also fine
  (;still ok
   lambda (x
            ;also cool
            )
   (print "hello world";you guessed it: fine
          )))

; comments ending the file shouldn't cause any problems either

M src/parse.c => src/parse.c +30 -16
@@ 26,10 26,12 @@ typedef struct {
		return r;                     \
	} while(0)

#define COMMENT ';'

result lisp_parse_value(lisp_runtime *rt, char *input, int index);

result lisp_parse_integer(lisp_runtime *rt, char *input, int index)
static result lisp_parse_value(lisp_runtime *rt, char *input, int index);

static result lisp_parse_integer(lisp_runtime *rt, char *input, int index)
{
	int n;
	lisp_integer *v = (lisp_integer*)lisp_new(rt, type_integer);


@@ 37,7 39,23 @@ result lisp_parse_integer(lisp_runtime *rt, char *input, int index)
	return_result(v, index + n);
}

char lisp_escape(char escape)
static int skip_space_and_comments(char *input, int index)
{
	for (;;) {
		while (isspace(input[index])) {
			index++;
		}
		if (input[index] && input[index] == COMMENT) {
			while (input[index] && input[index] != '\n') {
				index++;
			}
		} else {
			return index;
		}
	}
}

static char lisp_escape(char escape)
{
	switch (escape) {
	case 'a':


@@ 59,7 77,7 @@ char lisp_escape(char escape)
	}
}

result lisp_parse_string(lisp_runtime *rt, char *input, int index)
static result lisp_parse_string(lisp_runtime *rt, char *input, int index)
{
	int i;
	struct charbuf cb;


@@ 82,12 100,12 @@ result lisp_parse_string(lisp_runtime *rt, char *input, int index)
	return_result(str, i);
}

result lisp_parse_list_or_sexp(lisp_runtime *rt, char *input, int index)
static result lisp_parse_list_or_sexp(lisp_runtime *rt, char *input, int index)
{
	result r;
	lisp_list *rv, *l;

	while (isspace(input[index])) {index++;}
	index = skip_space_and_comments(input, index);
	if (input[index] == ')') {
		return_result(lisp_nil_new(rt), index + 1);
	}


@@ 99,9 117,7 @@ result lisp_parse_list_or_sexp(lisp_runtime *rt, char *input, int index)
	l = rv;

	while (true) {
		while (isspace(input[index])) {
			index++;
		}
		index = skip_space_and_comments(input, index);

		if (input[index] == '.') {
			index++;


@@ 123,14 139,14 @@ result lisp_parse_list_or_sexp(lisp_runtime *rt, char *input, int index)
	}
}

result lisp_parse_symbol(lisp_runtime *rt, char *input, int index)
static result lisp_parse_symbol(lisp_runtime *rt, char *input, int index)
{
	int n = 0;
	lisp_symbol *s;

	while (input[index + n] && !isspace(input[index + n]) &&
	       input[index + n] != ')' && input[index + n] != '.' &&
	       input[index + n] != '\'') {
	       input[index + n] != '\'' && input[index + n] != COMMENT) {
		n++;
	}
	s = (lisp_symbol*)lisp_new(rt, type_symbol);


@@ 140,18 156,16 @@ result lisp_parse_symbol(lisp_runtime *rt, char *input, int index)
	return_result(s, index + n);
}

result lisp_parse_quote(lisp_runtime *rt, char *input, int index)
static result lisp_parse_quote(lisp_runtime *rt, char *input, int index)
{
	result r = lisp_parse_value(rt, input, index + 1);
	r.result = lisp_quote(rt, r.result);
	return r;
}

result lisp_parse_value(lisp_runtime *rt, char *input, int index)
static result lisp_parse_value(lisp_runtime *rt, char *input, int index)
{
	while (isspace(input[index])) {
		index++;
	}
	index = skip_space_and_comments(input, index);

	if (input[index] == '"') {
		return lisp_parse_string(rt, input, index);