Skip to content

Commit

Permalink
v0.2.0 - minor refactor, performance improvements, immutable vectors …
Browse files Browse the repository at this point in the history
…etc...
  • Loading branch information
nickwanninger committed Jan 30, 2019
1 parent 88d239c commit 364c2eb
Show file tree
Hide file tree
Showing 69 changed files with 8,899 additions and 1,720,112 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ include_directories(/usr/local/include)

set (CMAKE_CXX_STANDARD 17)

# set(CMAKE_CXX_FLAGS_DEBUG "-g")
# set(CMAKE_CXX_FLAGS_RELEASE "-O3 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DBUILD_MODE=Debug")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -g -DBUILD_MODE=Release")


include_directories(${CMAKE_SOURCE_DIR}/include)
Expand All @@ -23,7 +23,7 @@ target_compile_options(cedar-lib PRIVATE -g -Wall -Wextra -Wno-unused-parameter

add_executable(cedar-bin src/main.cpp)

target_link_libraries(cedar-bin cedar-lib ${CMAKE_DL_LIBS})
target_link_libraries(cedar-bin cedar-lib ${CMAKE_DL_LIBS} -luv)
set_target_properties(cedar-bin PROPERTIES OUTPUT_NAME cedar)

target_compile_options(cedar-bin PRIVATE -Wall -Wextra -Wno-unused-parameter -flto)
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ CXXSRCFILES := $(filter %.cc,$(CODEFILES))

.PHONY: clean install libinc

BUILDMODE=Release

BINDIR = bin

default: src/lib/std.inc.h
@mkdir -p $(BINDIR)
@cd $(BINDIR); cmake -DCMAKE_BUILD_TYPE=Debug ../; make --no-print-directory


release: src/lib/std.inc.h
@mkdir -p $(BINDIR)
@cd $(BINDIR); cmake -DCMAKE_BUILD_TYPE=Release ../; make --no-print-directory

src/lib/std.inc.h: ./src/lib/std.inc.cdr
xxd -i src/lib/std.inc.cdr > src/lib/std.inc.h

gen:
@python3 tools/scripts/generate_cedar_h.py
@python3 tools/scripts/generate_src_cmakelists.py
@python3 tools/scripts/generate_opcode_h.py

Expand Down
16 changes: 0 additions & 16 deletions check.py

This file was deleted.

232 changes: 85 additions & 147 deletions example/example.cdr
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

(def range-step (fn (a b step)
(if (< a b) (cons a (range-step (+ a step) b step)))))


(def range (fn (a b)
(if (< a b) (cons a (range-step (+ 1 a) b 1)))))
(def (range a b)
(if (< a b) (cons a (range-step (+ 1 a) b 1))))

;; reduce the function f over xs starting at i
(def reduce (fn (f i xs)
(if (nil? xs)
i
(reduce f (f i (first xs)) (rest xs)))))
(def (reduce f i xs)
(if (nil? xs)
i
(reduce f (f i (first xs)) (rest xs))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -37,77 +38,6 @@



(defn inc (n) (+ n 1))
(defn dec (n) (- n 1))


(defmacro class (name & fields)
`(do
,(if (not (symbol? name)) (throw (str "Invalid class name: " name)))
;; define the name as a global class
(def ,name (cedar/make-class ',name))
;; map over each of the fields, expand them
;; and convert the defs as class fields
(do ,@(map (fn (mexpr)
(if (not (list? mexpr)) (throw (str "invalid class field: " mexpr))
(let ((expr (macroexpand mexpr)))
(cond (= 'def (first expr))
`(cedar/register-class-field ,name
',(first (rest expr))
,(first (rest (rest expr))))
(= :extends (first expr)) :EXTENDS
:else (throw (str "Invalid class field: " mexpr))))))
fields))))



;; since setting dict entries with keys as idents is
;; so common, this macro automatically quotes the key
;; identifier so you dont have to think about it.
(defmacro setq (coll name val)
`(set ,coll (quote ,name) ,val))

;; define a macro for use inside classes to clean up
;; the (set self 'name val) form. Cause it's ugly.
(defmacro set-self (name val)
`(set self (quote ,name) ,val))

;; the stream class is a basic io class
;; it offers .write and .read
(class stream
(def write nil)
(def read nil))

(class file-descriptor (:extends steam)
(def fd nil)
;; constructor that
(def (new self fd_arg)
(set-self fd fd_arg))
(def (write self s)
(os-write self.fd s))
(def (read self n)
(os-read self.fd n)))



(def (fopen-flags path flags)
(let ((fd (os-open path flags)))
(cond (= fd -1) (throw (str "unable to open file '" path "'"))
:otherwise (new file-descriptor fd))))

(def (fopen path)
(fopen-flags path (bit-or O_RDWR O_APPEND O_CREAT)))


(def file (fopen "file.txt"))




(def (join l s)
(reduce (fn (a b) (str a s b)) (str (first l)) (rest l)))





Expand Down Expand Up @@ -161,81 +91,89 @@
(def (first self) self.lo)
;; overload rest
(def (rest self)
(let ((lo self.lo)
(hi self.hi)
(st self.st))
(let [lo self.lo
hi self.hi
st self.st]
(println "REST")
(if (or (= hi :inf) (not (> hi lo)))
(new lazy-range (+ lo st) hi st)
:wqw))))

(def (step lo hi st)
(new lazy-range lo hi st))


(def (pos n)
(lazily (cons n (pos (inc n)))))

(def positive-numbers (pos 1))
(def (square x) (* x x))

(def (consume-lazy l)
(map (fn (x) x) l))



(class assertion-error
(def (new self msg) (set self 'msg msg)))

(defmacro assert (claim)
(list 'when-not claim
(list 'throw (list 'assertion-error (str claim)))))


(defmacro comment (& body)
nil)


(defmacro case (check & cases)
(let ((sym (gensym)))
(list 'let (list (list sym check)))))


(def each
(fn (f l)
(if (not (nil? l))
(do (f (first l))
(each f (rest l))))))


(def (drop n l)
(if (= n 0) l
(drop (dec n) (rest l))))

(def (take n l)
(if (= n 0) nil
(lazily (cons (first l) (take (dec n) (rest l))))))


(def (infinite-list-from ind)
(lazily (cons ind (range (inc ind)))))

(def (step-inf lo st)
(lazily (cons lo (step-inf (inc lo) (+ lo st)))))

(def (range lo & xs)
(let ((nx (first xs))
(hi (second xs)))
(cond (nil? nx) (if (= lo :inf) (step-inf 0 1) (step 0 (dec lo) 1))
(nil? hi) (if (= nx :inf) (step-inf lo 1) (step lo nx 1))
(= hi :inf) (step-inf lo (- nx lo))
:else (step lo hi (- nx lo)))))


(def (filter pred coll)
(lazily
(let ((s (seq coll)))
(let ((f (first s)) (r (rest s)))
(if (pred f)
(cons f (filter pred r))
(filter pred r))))))
(defmacro struct (id & fields)
(do
(println fields)
(let [fsets (map (fn (fname)
`(set-self ,fname ,fname))
fields)]
`(class ,id
(def (new self ,@fields)
(do ,@fsets))))))


;; TODO: add this to the stdlib
(def (contains? coll k)
(cond (nil? coll) nil
(= (first coll) k) true
:otherwise (recur (rest coll) k)))

;; TODO: add to stdlib
(def (contains-where? coll pred)
(cond (nil? coll) nil
(pred (first coll)) true
:otherwise (recur (rest coll) pred)))

;; TODO: add all flavours to stdlib
(def (contains-number? coll)
(contains-where? coll number?))


(def (hexchar x)
(get "0123456789ABCDEF" (bit-and x 0x0F)))

(def (hex x)
(when (number? x)
(cond (= 0 x) ""
:else (str (hex (bit-shift-right x 4))
(hexchar (bit-and 0x0F x))))))




(class c-extension
(def name "ext")
(def c-files nil)
(def (new self name c-files)
(do
(set-self name name)
(set-self c-files c-files))))


(class base
(def (new self)
(set-self foo 30)))

(class derived
(:extends base)
(def (new self)
(println self)))

(def (vrange n)
(let [i 0
v []]
(if (< i n)
(recur (inc i) (cons i v))
v)))

(def (memoize f)
(let [mem {}
not-found-sym (gensym)]
(fn (& args)
(let [r (get mem args not-found-sym)]
(if (= r not-found-sym)
(set mem args (apply f args))
r)))))

(def memfib (memoize fib))
Empty file removed file.txt
Empty file.
46 changes: 28 additions & 18 deletions include/cedar.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


/*
* MIT License
*
Expand All @@ -22,39 +24,47 @@
* SOFTWARE.
*/


// generated by tools/scripts/generate_cedar_h.py
#pragma once

#ifndef CEDAR_HH
#define CEDAR_HH

#include "cedar/version.h"
#include "cedar/types.h"
#include "cedar/parser.h"
#include "cedar/vm/bytecode.h"
#include "cedar/object_types.h"
#include "cedar/passes.h"
#include "cedar/memory.h"
#include "cedar/object.h"
#include "cedar/runes.h"
#include "cedar/with_meta.h"
#include "cedar/fractional.h"
#include "cedar/ref.h"
#include "cedar/vm/compiler.h"
#include "cedar/vm/machine.h"
#include "cedar/vm/bytecode.h"
#include "cedar/vm/instruction.h"
#include "cedar/object_types.h"
#include "cedar/object/symbol.h"
#include "cedar/vm/binding.h"
#include "cedar/vm/opcode.h"
#include "cedar/object/keyword.h"
#include "cedar/object/symbol.h"
#include "cedar/object/invocable.h"
#include "cedar/object/nil.h"
#include "cedar/object/list.h"
#include "cedar/object/user_type.h"
#include "cedar/object/lambda.h"
#include "cedar/object/dict.h"
#include "cedar/object/sequence.h"
#include "cedar/object/lazy_seq.h"
#include "cedar/object/typeobject.h"
#include "cedar/object/bytes.h"
#include "cedar/object/indexable.h"
#include "cedar/object/lazy_seq.h"
#include "cedar/object/number.h"
#include "cedar/object/lambda.h"
#include "cedar/object/string.h"
#include "cedar/object/dict.h"
#include "cedar/object/vector.h"
#include "cedar/object/user_type.h"
#include "cedar/memory.h"
#include "cedar/object.h"
#include "cedar/runes.h"
#include "cedar/util.hpp"
#include "cedar/vm/opcode.h"
#include "cedar/exception.hpp"
#include "cedar/ref.h"
#include "cedar/vm/binding.h"
#include "cedar/types.h"
#include "cedar/object/fiber.h"
#include "cedar/object/string.h"
#include "cedar/lib/linenoise.h"

#endif
#endif // CEDAR_HH
Loading

0 comments on commit 364c2eb

Please sign in to comment.