forked from joxa/joxa
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jordan Wilberding <diginux@gmail.com>
- Loading branch information
1 parent
9bc068a
commit 895955f
Showing
3 changed files
with
175 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
;;; The Joxa Core Library | ||
;;; ===================== | ||
;;; * author: Eric Merritt | ||
;;; * copyright: Erlware, LLC 2011 - 2012 | ||
;;; | ||
;;; Licensed under the Apache License, Version 2.0 you may not use | ||
;;; this file except in compliance with the License. You may obtain a | ||
;;; copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
;;; | ||
;;; | ||
;;; This module provides for the definition and abstraction of structs | ||
;;; in joxa. | ||
(module joxa.records | ||
(require (joxa.core :as core) | ||
lists erlang) | ||
(use (erlang :only (+/2 | ||
==/2)))) | ||
|
||
(defn convert-to-record-description (descs) | ||
(case (lists/foldl (fn (desc acc0) | ||
(case acc0 | ||
({acc1 count} | ||
(case desc | ||
(d (when (erlang/is_atom d)) | ||
{({d count :undefined} . acc1) | ||
(core/incr count)}) | ||
({name default} | ||
{({name count default} . acc1) | ||
(core/incr count)}))))) | ||
{[], 2} descs) | ||
({acc _} | ||
(lists/reverse acc)))) | ||
|
||
(defn create-elements (decl descs) | ||
(let (obj (core/gensym) | ||
name (core/gensym) | ||
value (core/gensym)) | ||
[`(~decl get-element ( ~obj ~name) | ||
(case ~obj | ||
~@(lists/map (fn (desc) | ||
(case desc | ||
({f-name count _} | ||
`('~f-name | ||
(erlang/element ~count ~obj))))) descs) | ||
(_ | ||
(erlang/throw {:invalid-field ~name})))) | ||
`(~decl set-element ( ~obj ~name ~value) | ||
(case ~obj | ||
~@(lists/map (fn (desc) | ||
(case desc | ||
({f-name count _} | ||
`('~f-name | ||
(erlang/setelement ~count ~obj ~value))))) descs) | ||
(_ | ||
(erlang/throw {:invalid-field ~name}))))])) | ||
|
||
(defn create-record (decl descs) | ||
(let (ells (create-elements decl descs) | ||
; costr (create-constructors decl descs) | ||
; field-info (create-field-info decl descs) | ||
; accessors (create-accessors decl descs) | ||
; t (create-templates decl descs) | ||
; withs (create-withs decl descs) | ||
) | ||
`(do ~@ells))) | ||
|
||
(defmacro+ defrecord+ (field-descriptions) | ||
(create-record :defn+ (convert-to-record-description field-descriptions))) | ||
|
||
(defmacro+ defrecord (field-descriptions) | ||
(create-record :defn (convert-to-record-description field-descriptions))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
-module(jxat_records). | ||
|
||
-export([given/3, 'when'/3, then/3]). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
given([a,module,that,has,defined,recordss], _State, _) -> | ||
Source1 = <<" | ||
(module jxat-records-def-test) | ||
(defrecords+ my-records (name age {sex male} {address \"unknown\"})) | ||
(defrecords+ other one) | ||
">>, | ||
|
||
{ok, Source1}; | ||
given([another,module,uses,those,recordss], Source1, _) -> | ||
Source2 = <<" | ||
(module jxat-records-uses-test | ||
(require jxat-records-def-test)) | ||
(defn+ create1 () | ||
{(jxat-records-def-test/my-records | ||
\"Robert\" | ||
1024 | ||
:male | ||
\"Somewhere in Ireland\"), | ||
(jxat-records-def-test/my-records | ||
[{:name \"Robert\"} | ||
{:age 1024} | ||
{:sex :male} | ||
{:address \"Somewhere in Ireland\"}])}) | ||
(defn+ create2() | ||
{(jxat-records-def-test/other 1) | ||
(jxat-records-def-test/other [{:one 1}])}) | ||
(defn+ match1 (one two) | ||
(case one | ||
((jxat-records-def-test/t two [{:name \"Robert\"}]) | ||
:matched) | ||
(_ | ||
:did-not-match))) | ||
(defn+ match2 (one two) | ||
(case one | ||
((jxat-records-def-test/t two [{:name \"Robert\"} | ||
{:sex :male}]) | ||
:matched) | ||
(_ | ||
:did-not-match))) | ||
(defn+ match2 (one two) | ||
(case one | ||
((jxat-records-def-test/t two [{:name \"Rob\"} | ||
{:sex :male}]) | ||
:matched) | ||
(_ | ||
:did-not-match))) | ||
(defn+ with (one) | ||
(jxat-records-def-test/with-fields [{name local-name} | ||
{age local-age} | ||
{address local-address}] | ||
{local-name local-age local-address}))">>, | ||
|
||
{ok, {Source1, Source2}}. | ||
|
||
'when'([joxa,is,called,on,these,modules], {Source1, Source2}, _) -> | ||
Result1 = joxa.compiler:forms(Source1, []), | ||
Result2 = joxa.compiler:forms(Source2, []), | ||
{ok, {Result1, Result2}}. | ||
|
||
then([a,beam,binary,is,produced,for,both], {Ctx1, Ctx2}, _) -> | ||
?assertMatch(true, is_binary(joxa.compiler:'get-context'(result, Ctx1))), | ||
?assertMatch(true, is_binary(joxa.compiler:'get-context'(result, Ctx2))), | ||
?assertMatch(false, joxa.compiler:'has-errors?'(Ctx1)), | ||
?assertMatch(false, joxa.compiler:'has-errors?'(Ctx2)), | ||
{ok, ok}; | ||
then([the,described,function,can,be,called,'and',works,correctly], State, _) -> | ||
|
||
?assertMatch([{'--joxa-info',1}, | ||
{'--joxa-info',2}, | ||
|
||
{module_info,0}, | ||
{module_info,1}], | ||
lists:sort('jxat-records-def-test':module_info(exports))), | ||
{ok, State}. | ||
|
||
|