Merge pull request #918 from hellerve/veit/static-args

core: use static array for args
This commit is contained in:
Erik Svedäng 2020-08-26 20:57:56 +02:00 committed by GitHub
commit ec20679440
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 61 deletions

View File

@ -16,10 +16,8 @@
(register sleep-micros (Fn [Int] ()))
(doc system "Performs a system command.")
(register system (Fn [&String] Int))
(doc get-arg "Gets the command line argument at a specified index.")
(register get-arg (Fn [Int] (Ref String)))
(doc get-args-len "Gets the number of command line arguments.")
(register get-args-len (Fn [] Int))
(doc args "Represents the command line arguments.")
(register args (StaticArray String))
(register fork (Fn [] Int) "fork")
(register wait (Fn [(Ptr Int)] Int) "wait")
(register get-exit-status (Fn [Int] Int) "WEXITSTATUS")

View File

@ -183,7 +183,7 @@ Array String_to_MINUS_bytes(const String *s) {
String String_from_MINUS_bytes(Array *a) {
String s;
const char *us = (const char *)a->data;
s = CARP_MALLOC(a->len+1);
s = CARP_MALLOC(a->len + 1);
memcpy(s, us, a->len);
s[a->len] = '\0';
return s;

View File

@ -40,12 +40,3 @@ int System_system(const String *command) {
}
Array System_args;
String *System_get_MINUS_arg(int idx) {
assert(idx < System_args.len);
return &(((String *)System_args.data)[idx]);
}
int System_get_MINUS_args_MINUS_len() {
return System_args.len;
}

View File

@ -523,6 +523,26 @@
</p>
</div>
<div class="binder">
<a class="anchor" href="#args">
<h3 id="args">
args
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(StaticArray String)
</p>
<span>
</span>
<p class="doc">
<p>Represents the command line arguments.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#carp-init-globals">
<h3 id="carp-init-globals">
@ -621,46 +641,6 @@
</p>
</div>
<div class="binder">
<a class="anchor" href="#get-arg">
<h3 id="get-arg">
get-arg
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(Fn [Int] (Ref String a))
</p>
<span>
</span>
<p class="doc">
<p>Gets the command line argument at a specified index.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#get-args-len">
<h3 id="get-args-len">
get-args-len
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(Fn [] Int)
</p>
<span>
</span>
<p class="doc">
<p>Gets the number of command line arguments.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#get-exit-status">
<h3 id="get-exit-status">

View File

@ -39,6 +39,19 @@ import qualified Meta
import Debug.Trace
-- | Dynamic (REPL) evaluation of XObj:s (s-expressions)
-- Note: You might find a bunch of code of the following form both here and in
-- macroExpand:
--
-- return (ctx, do res <- <something>
-- Right <something else with res>)
--
-- This might a little weird to you, and rightfully so. Through the nested do
-- we ensure that an evaluation is forced where it needs to be, since we depend
-- on the state here; eval is inherently stateful (because it carries around
-- the compilers context, which might change after each macro expansion), and
-- it gets real weird with laziness. (Note to the note: this code is mostly a
-- remnant of us using StateT, and might not be necessary anymore since we
-- switched to more explicit state-passing.)
eval :: Context -> XObj -> IO (Context, Either EvalError XObj)
eval ctx xobj@(XObj o i t) =
case o of
@ -75,6 +88,10 @@ eval ctx xobj@(XObj o i t) =
(newCtx, evaled) <- foldlM successiveEval (ctx, Right []) objs
return (newCtx, do ok <- evaled
Right (XObj (Arr ok) i t))
StaticArr objs -> do
(newCtx, evaled) <- foldlM successiveEval (ctx, Right []) objs
return (newCtx, do ok <- evaled
Right (XObj (StaticArr ok) i t))
_ -> return (ctx, Right xobj)
where
resolveDef (XObj (Lst [XObj DefDynamic _ _, _, value]) _ _) = value
@ -293,6 +310,10 @@ macroExpand ctx xobj =
(newCtx, expanded) <- foldlM successiveExpand (ctx, Right []) objs
return (newCtx, do ok <- expanded
Right (XObj (Arr ok) i t))
XObj (StaticArr objs) i t -> do
(newCtx, expanded) <- foldlM successiveExpand (ctx, Right []) objs
return (newCtx, do ok <- expanded
Right (XObj (StaticArr ok) i t))
XObj (Lst [XObj (Lst (XObj Macro _ _:_)) _ _]) _ _ -> eval ctx xobj
XObj (Lst (x@(XObj sym@(Sym s _) _ _):args)) i t -> do
(newCtx, f) <- eval ctx x

View File

@ -12,10 +12,6 @@
(deftest test
(assert-equal test
1
(System.get-args-len)
"System.get-args-len works as expected")
(assert-equal test
(make-exe-path "Untitled")
(System.get-arg 0)
"System.get-arg works as expected"))
$[@(make-exe-path "Untitled")]
&System.args
"System.args works as expected"))