Skip to content

Commit

Permalink
[master] add quote form (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhrr committed Apr 16, 2015
1 parent 3b9bf7b commit 7b709c6
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ add_executable (dalec src/dale/dalec.cpp
src/dale/Form/Proc/VaEnd/VaEnd.cpp
src/dale/Form/Proc/VaArg/VaArg.cpp
src/dale/Form/Proc/Null/Null.cpp
src/dale/Form/Proc/GetDNodes/GetDNodes.cpp
src/dale/Form/Proc/Quote/Quote.cpp
src/dale/Form/Proc/Def/Def.cpp
src/dale/Form/Proc/NullPtr/NullPtr.cpp
src/dale/Form/Proc/Do/Do.cpp
Expand Down
5 changes: 5 additions & 0 deletions doc/1-11-form-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ Adds the specified namespace to the list of namespaces used for
lookup, and evaluates each of the forms in that context. As per `do`,
the result is that of the final form.

#### (`q` {`form`})

Converts the form into a pointer to `DNode` (static), and returns that
pointer.

#### Other

Variables and structs may be defined within procedures in the same way
Expand Down
10 changes: 10 additions & 0 deletions doc/1-7-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ As per functions, the last value evaluated within the body of the
macro is returned implicitly, in the absence of an explicit `return`
instruction.

The quote (`q`) core form allows for converting an arbitrary form into
a pointer to a `DNode` (static). For example:

(def const-string (macro extern (void) (q (p (const char)))))

There are two principal standard libraries that deal with macros:
[`macros-core`](./2-4-macros-core.md) and [`macros`](./2-6-macros.md).
The former provides many functions that ease macro authorship. The
Expand All @@ -98,6 +103,11 @@ a `qq` form. There is a corresponding `uql` form ('unquote-list'),
that takes the list from the specified `DNode` and 'splices' it into
place.

Note that `qq` 'wraps' its arguments, whereas `q` does not:

(qq p (const char)) -> (p const char)
(q (p (const char))) -> (p const char)

### Typed macros

A macro may specify one or more typed parameters, starting from the
Expand Down
15 changes: 15 additions & 0 deletions doc/all.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@ As per functions, the last value evaluated within the body of the
macro is returned implicitly, in the absence of an explicit `return`
instruction.

The quote (`q`) core form allows for converting an arbitrary form into
a pointer to a `DNode` (static). For example:

(def const-string (macro extern (void) (q (p (const char)))))

There are two principal standard libraries that deal with macros:
[`macros-core`](#macros-core) and [`macros`](#macros).
The former provides many functions that ease macro authorship. The
Expand All @@ -706,6 +711,11 @@ a `qq` form. There is a corresponding `uql` form ('unquote-list'),
that takes the list from the specified `DNode` and 'splices' it into
place.

Note that `qq` 'wraps' its arguments, whereas `q` does not:

(qq p (const char)) -> (p const char)
(q (p (const char))) -> (p const char)

### Typed macros

A macro may specify one or more typed parameters, starting from the
Expand Down Expand Up @@ -1065,6 +1075,11 @@ Adds the specified namespace to the list of namespaces used for
lookup, and evaluates each of the forms in that context. As per `do`,
the result is that of the final form.

#### (`q` {`form`})

Converts the form into a pointer to `DNode` (static), and returns that
pointer.

#### Other

Variables and structs may be defined within procedures in the same way
Expand Down
2 changes: 1 addition & 1 deletion modules/macros.dt
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ be used again.
(label end-loop2)
(let ((res \ (bqq do (new-scope
(def (uq vn) (var auto (p DNode) (std.macros.copy mc true
(get-dnodes (uq-nc holder)))))
(q (uq-nc holder)))))
(def firstnode (var auto (p DNode)))
(def trailing (var auto (p DNode)))
(def newnode (var auto (p DNode)))
Expand Down
6 changes: 3 additions & 3 deletions src/dale/CoreForms/CoreForms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "../Form/Proc/VaEnd/VaEnd.h"
#include "../Form/Proc/VaArg/VaArg.h"
#include "../Form/Proc/Null/Null.h"
#include "../Form/Proc/GetDNodes/GetDNodes.h"
#include "../Form/Proc/Quote/Quote.h"
#include "../Form/Proc/Def/Def.h"
#include "../Form/Proc/NullPtr/NullPtr.h"
#include "../Form/Proc/Do/Do.h"
Expand Down Expand Up @@ -63,7 +63,7 @@ std::set<std::string> core_forms_no_override;

const int core_forms_no_override_max = 31;
const char *core_forms_no_override_strs[core_forms_no_override_max + 1] = {
"goto", "label", "return", ":", "get-dnodes", "p=", "p+", "p-", "p<",
"goto", "label", "return", ":", "q", "p=", "p+", "p-", "p<",
"p>", "def", "if", "null", "nullptr", "do", "cast", "va-arg",
"va-start", "va-end",
"sizeof", "offsetof", "alignmentof", "funcall", "using-namespace",
Expand Down Expand Up @@ -99,7 +99,7 @@ init()
ADD_SC("va-start", &FormProcVaStartParse);
ADD_SC("va-end", &FormProcVaEndParse);
ADD_SC("null", &FormProcNullParse);
ADD_SC("get-dnodes", &FormProcGetDNodesParse);
ADD_SC("q", &FormProcQuoteParse);
ADD_SC("def", &FormProcDefParse);
ADD_SC("nullptr", &FormProcNullPtrParse);
ADD_SC("do", &FormProcDoParse);
Expand Down
28 changes: 0 additions & 28 deletions src/dale/Form/Proc/GetDNodes/GetDNodes.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ IntNodeToStaticDNode(Units *units, Node *node, llvm::Value *next_node)
}

bool
FormProcGetDNodesParse(Units *units, Function *fn, llvm::BasicBlock *block,
Node *node, bool get_address, bool prefixed_with_core,
ParseResult *pr)
FormProcQuoteParse(Units *units, Function *fn, llvm::BasicBlock *block,
Node *node, bool get_address, bool prefixed_with_core,
ParseResult *pr)
{
Context *ctx = units->top()->ctx;

Expand All @@ -189,7 +189,7 @@ FormProcGetDNodesParse(Units *units, Function *fn, llvm::BasicBlock *block,
llvm_type_dnode = (llvm::Type *) intptr_value;
}

if (!ctx->er->assertArgNums("get-dnodes", node, 1, 1)) {
if (!ctx->er->assertArgNums("q", node, 1, 1)) {
return false;
}

Expand Down
25 changes: 25 additions & 0 deletions src/dale/Form/Proc/Quote/Quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef DALE_FORM_PROC_QUOTE
#define DALE_FORM_PROC_QUOTE

namespace dale
{
/*! Parse a procedure-body quote statement.
* @param units The units context.
* @param fn The function currently in scope.
* @param block The current block.
* @param node The node being parsed.
* @param get_address Whether to return the address of the result.
* @param prefixed_with_core (Unused by this form.)
* @param pr The parse result for the response.
*
* This takes an arbitrary form and returns a pointer to a static
* DNode representing that form. It is similar to ' in CL/Scheme,
* except that there's no concept of a 'symbol' as such here.
*/
bool
FormProcQuoteParse(Units *units, Function *fn, llvm::BasicBlock *block,
Node *node, bool get_address, bool prefixed_with_core,
ParseResult *pr);
}

#endif
24 changes: 24 additions & 0 deletions t/002macro/020quote.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/perl

use warnings;
use strict;
$ENV{"DALE_TEST_ARGS"} ||= "";
my $test_dir = $ENV{"DALE_TEST_DIR"} || ".";
$ENV{PATH} .= ":.";

use Data::Dumper;
use Test::More tests => 3;

my @res = `dalec $ENV{"DALE_TEST_ARGS"} $test_dir/t/src/quote.dt -o quote `;
is_deeply(\@res, [], 'No compilation errors');
@res = `./quote`;
is($?, 0, 'Program executed successfully');

chomp for @res;

is_deeply(\@res, [ 'string: Yo dude' ],
'Got correct results');

`rm quote`;

1;
16 changes: 16 additions & 0 deletions t/src/quote.dt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(import macros)
(import cstdio)
(import introspection)

(using-namespace std.macros
(def def-type-macro (macro extern (new linkage old)
(qq using-namespace std.macros
(def (uq new) (macro (uq linkage) (void)
(q (uq old))))))))

(def-type-macro cstring extern (p (const char)))

(def main (fn extern-c int (void)
(def a-string (var auto (cstring) "Yo dude"))
(printf "string: %s\n" a-string)
0))
2 changes: 1 addition & 1 deletion vim/dale.vim
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ syn keyword lispTodo contained combak combak: todo todo:
syn keyword lispFunc goto if label return setf @ : # $
syn keyword lispFunc p+ p- p= p< p>
syn keyword lispFunc va-arg va-start va-end
syn keyword lispFunc null get-dnodes def nullptr do cast
syn keyword lispFunc null q def nullptr do cast
syn keyword lispFunc sizeof offsetof alignmentof
syn keyword lispFunc funcall using-namespace new-scope array-of
syn keyword lispFunc setv @$ :@ @: @:@
Expand Down

0 comments on commit 7b709c6

Please sign in to comment.