Skip to content

Commit

Permalink
Version 0.86: Improved lists
Browse files Browse the repository at this point in the history
Also updates the stress_threads example based on performance issues.
  • Loading branch information
andrakis committed Feb 19, 2018
1 parent d01a25b commit 4c18b55
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion PocoLithp/PocoLithp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "stdafx.h"
#include <Stackless.hpp> // Ugly hack

#define ELISP_VERSION "0.85"
#define ELISP_VERSION "0.86"

// Undefine to use recursive emulator
#define ELISP_STACKLESS
Expand Down
42 changes: 33 additions & 9 deletions PocoLithp/stdlib/ELlists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ using namespace PocoLithp::Stackless;


LithpCell proc_empty(const LithpCells &c) {
if(c.size() == 0) return LithpCell(Var, 0);
if(c.empty()) return LithpCell(Var, 0);
return c[0].empty() ? sym_true : sym_false;
}

LithpCell proc_length(const LithpCells &c) {
if(c.size() == 0) return LithpCell(Var, 0);
if(c.empty()) return LithpCell(Var, 0);
return LithpCell(Var, c[0].size());
}
LithpCell proc_nullp(const LithpCells &c) {
if(c.size() == 0) return sym_true;
if(c.empty()) return sym_true;
return c[0].is_nullp() ? sym_true : sym_false;
}
LithpCell proc_head(const LithpCells &c) {
if(c.size() == 0) return sym_nil;
if(c[0].size() == 0) return sym_nil;
if(c.empty()) return sym_nil;
if(c[0].empty()) return sym_nil;
return c[0][0];
}

LithpCell proc_tail(const LithpCells &c)
{
if (c.size() == 0)
if(c.empty())
return LithpCell(List, LithpCells());
LithpCells result = c[0].list();
if(result.size() == 0)
if(result.empty())
return LithpCell(List, LithpCells());
result.erase(result.begin());
return LithpCell(List, result);
}

LithpCell proc_append(const LithpCells &c)
{
if (c.size() == 0)
if(c.empty())
return LithpCell(List, LithpCells());
else if(c.size() == 1)
return LithpCell(List, c[0].list());
Expand All @@ -53,7 +53,7 @@ LithpCell proc_append(const LithpCells &c)

LithpCell proc_cons(const LithpCells &c)
{
if (c.size() == 0)
if(c.empty())
return LithpCell(List, LithpCells());
else if(c.size() == 1)
return LithpCell(List, c[0].list());
Expand All @@ -70,12 +70,36 @@ LithpCell proc_list(const LithpCells &c)
return LithpCell(List, c);
}

/** Lists module native inbuilt functions (NIF) */
namespace nif_lists {
/**
* Drop given Value from List.
*
* @param List list()
* @param Value any()
* @return list()
*/
LithpCell drop(const LithpCells &c) {
LithpCells result;
if(c.empty() || c.size() < 2)
return LithpCell(List, LithpCells());
const LithpCells &list_from = c[0].list();
const LithpCell &list_drop = c[1];
for (LithpCells::const_iterator it = list_from.cbegin(); it != list_from.cend(); ++it) {
if(*it != list_drop)
result.push_back(*it);
}
return LithpCell(List, result);
}
}

void Elispidae::Stdlib::init_lists() {
add_environment_runtime([](LithpEnvironment &env) {
env["append"] = LithpCell(&proc_append); env["head"] = LithpCell(&proc_head);
env["tail"] = LithpCell(&proc_tail); env["cons"] = LithpCell(&proc_cons);
env["length"] = LithpCell(&proc_length); env["list"] = LithpCell(&proc_list);
env["null?"] = LithpCell(&proc_nullp);
env["empty?"] = LithpCell(&proc_empty);
env["lists:drop"] = LithpCell(nif_lists::drop);
});
}
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Why?

An attempt at a much more lightweight implementation of a C++ Lithp (other attempt [Stackful](https://github.com/andrakis/Stackful) being rather more complicated than desired.)

It aims to be reasonably fast even on slower processors - one of its intended targets is OpenRISC 1000, specifically the [JavaSript OpenRISC 1000 emulator](https://jor1k.com). Other interpreted languages are available on OpenRISC, but the performance of these languages tends to be very slow.

It also aims to borrow several ideas from different programming languages, rather than trying to port them to OpenRISC.

What?
-----

Expand Down Expand Up @@ -111,15 +115,15 @@ How?
Status
------

**Version: 0.85**
**Version: 0.86**

**Language compatibility level:** Lisp-ish, with macros.

Mainly Lisp-like syntax, mixed with Lithp (Variables are introduced, `#` is synonym for `lambda`).

Microthreading, message passing, and scheduling is working.
Microthreading, message passing, and scheduling is working.

Tail-call optimization is presently not working.
Tail-call optimization has once again been dropped. Frame optimizations are the future development target.

Building
========
Expand Down
6 changes: 2 additions & 4 deletions samples/stress_threads.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; because it is a much slower platform.
(define ThreadCount
(if (== or1k (arch))
15
10
50
)
)
Expand All @@ -24,8 +24,6 @@
(# (Ele Acc)
(if (Predicate Ele) (+ Acc (list Ele)) Acc)))
))
(define lists:drop (# (List Value)
(lists:filter List (# (Ele) (!= Ele Value)))))

;; Helper function to spawn many threads with given Code and Argument.
;; Returns list of thread references.
Expand Down Expand Up @@ -58,7 +56,7 @@
(begin
(define Thread (head ThreadsStarted))
(print "Requesting factorial " N "from" Thread)
(send (list N (self)) Thread)
(send (list (+ N 5) (self)) Thread)
(message-loop (tail ThreadsStarted) (+ (list Thread) ThreadsRunning) (+ N 1))
)
;; else, sent messages to all threads, get responses.
Expand Down

0 comments on commit 4c18b55

Please sign in to comment.