Changed type of member getter method to always returning a Ref (used to be non-ref for primitive types).

This commit is contained in:
Erik Svedäng 2018-01-26 21:23:29 +01:00
parent d67e3f0472
commit 936dd8b26c
13 changed files with 122 additions and 93 deletions

View File

@ -130,6 +130,20 @@ arrayModule = Env { envBindings = bindings, envParent = Nothing, envModuleName =
, templateSort
]
pointerModule :: Env
pointerModule = Env { envBindings = bindings, envParent = Nothing, envModuleName = Just "Pointer", envUseModules = [], envMode = ExternalEnv }
where bindings = Map.fromList [ templatePointerCopy ]
templatePointerCopy :: (String, Binder)
templatePointerCopy = defineTemplate
(SymPath ["Pointer"] "copy")
(FuncTy [RefTy (PointerTy (VarTy "p"))] (PointerTy (VarTy "p")))
(toTemplate "$p* $NAME ($p** ptrRef)")
(toTemplate $ unlines ["$DECL {"
," return *ptrRef;"
,"}"])
(const [])
dynamicStringModule :: Env
dynamicStringModule = Env { envBindings = bindings, envParent = Nothing, envModuleName = Just "String", envUseModules = [], envMode = ExternalEnv }
where bindings = Map.fromList [ addCommand "char-at" 2 commandCharAt
@ -192,6 +206,7 @@ startingGlobalEnv noArray =
, register "NULL" (VarTy "a")
]
++ (if noArray then [] else [("Array", Binder (XObj (Mod arrayModule) Nothing Nothing))])
++ [("Pointer", Binder (XObj (Mod pointerModule) Nothing Nothing))]
++ [("Dynamic", Binder (XObj (Mod dynamicModule) Nothing Nothing))]
@ -203,7 +218,7 @@ startingTypeEnv = Env { envBindings = bindings
, envMode = ExternalEnv
}
where bindings = Map.fromList
$ [ interfaceBinder "copy" (FuncTy [(RefTy (VarTy "a"))] (VarTy "a")) [SymPath ["Array"] "copy"] builtInSymbolInfo
$ [ interfaceBinder "copy" (FuncTy [(RefTy (VarTy "a"))] (VarTy "a")) [SymPath ["Array"] "copy", SymPath ["Pointer"] "copy"] builtInSymbolInfo
, interfaceBinder "str" (FuncTy [(VarTy "a")] StringTy) [SymPath ["Array"] "str"] builtInSymbolInfo
-- TODO: Implement! ("=", Binder (defineInterface "=" (FuncTy [(VarTy "a"), (VarTy "a")] BoolTy)))
]

View File

@ -4,5 +4,4 @@
(register time (Fn [] Int))
(register srand (Fn [Int] ()))
(register sleep-seconds (Fn [Int] ()))
(register sleep-micros (Fn [Int] ()))
)
(register sleep-micros (Fn [Int] ())))

View File

@ -78,8 +78,8 @@
(State.set-failed (State.set-passed state 0) 0))
(defn print-test-results [state]
(let [passed (State.passed state)
failed (State.failed state)]
(let [passed @(State.passed state)
failed @(State.failed state)]
(do
(IO.println "Results:")
(if (Int.> (Int.+ passed failed) 0)
@ -112,7 +112,7 @@
(defmacro with-test [name :rest forms]
(list 'let [name '&(Test.State.init 0 0)]
(cons-last
(list 'Test.State.failed name)
(list 'Int.copy (list 'Test.State.failed name))
(cons 'do (with-test-internal name forms)))))
(defmacro deftest [name state-name :rest forms]

View File

@ -5,10 +5,10 @@
(V2.init x y))
(defn get-x [o]
(V2.x o))
@(V2.x o))
(defn get-y [o]
(V2.y o))
@(V2.y o))
(defn set-x [o v]
(V2.set-x o v))
@ -17,7 +17,7 @@
(V2.set-y o v))
(defn to-string [o]
(string-join @"Vector2(" (str (V2.x o)) @", " (str (V2.y o)) @")"))
(string-join @"Vector2(" (str @(V2.x o)) @", " (str @(V2.y o)) @")"))
(defn zero []
(V2.init 0.0 0.0))
@ -26,35 +26,35 @@
(V2.init (random-between 0.0 1.0) (random-between 0.0 1.0)))
(defn add [a b]
(V2.init (+ (V2.x a) (V2.x b))
(+ (V2.y a) (V2.y b))))
(V2.init (+ @(V2.x a) @(V2.x b))
(+ @(V2.y a) @(V2.y b))))
(defn sub [a b]
(V2.init (- (V2.x a) (V2.x b))
(- (V2.y a) (V2.y b))))
(V2.init (- @(V2.x a) @(V2.x b))
(- @(V2.y a) @(V2.y b))))
(defn mul [a n]
(V2.init (* (V2.x a) n)
(* (V2.y a) n)))
(V2.init (* @(V2.x a) n)
(* @(V2.y a) n)))
(defn div [a n]
(V2.init (/ (V2.x a) n)
(/ (V2.y a) n)))
(V2.init (/ @(V2.x a) n)
(/ @(V2.y a) n)))
(defn = [a b]
(and (Double.= (V2.x a) (V2.x b))
(Double.= (V2.y a) (V2.y b))))
(and (Double.= @(V2.x a) @(V2.x b))
(Double.= @(V2.y a) @(V2.y b))))
(defn /= [a b]
(not (= a b)))
(defn approx [a b]
(and (Double.approx (V2.x a) (V2.x b))
(Double.approx (V2.y a) (V2.y b))))
(and (Double.approx @(V2.x a) @(V2.x b))
(Double.approx @(V2.y a) @(V2.y b))))
(defn mag-sq [o]
(let [x (V2.x o)
y (V2.y o)]
(let [x @(V2.x o)
y @(V2.y o)]
(+ (* x x) (* y y))))
(defn mag [o]
@ -71,7 +71,7 @@
(mag &s)))
(defn heading [a]
(Double.atan2 (V2.y a) (V2.x a)))
(Double.atan2 @(V2.y a) @(V2.x a)))
(defn rotate [a n]
(let [h (+ (heading a) n)
@ -79,8 +79,8 @@
(V2.init (* (Double.cos h) m) (* (Double.sin h) m))))
(defn dot [x y]
(+ (* (V2.x x) (V2.x y))
(* (V2.y x) (V2.y y))))
(+ (* @(V2.x x) @(V2.x y))
(* @(V2.y x) @(V2.y y))))
(defn angle-between [a b]
(let [dmm (/ (dot a b) (* (mag a) (mag b)))]
@ -96,8 +96,8 @@
(= (angle-between a b) (/ Double.pi 2.0)))
(defn lerp [a b amnt]
(init (* (- (V2.x b) (V2.x a)) amnt)
(* (- (V2.y b) (V2.y a)) amnt)))
(init (* (- @(V2.x b) @(V2.x a)) amnt)
(* (- @(V2.y b) @(V2.y a)) amnt)))
)
(defmodule Vector3
@ -107,8 +107,8 @@
(V3.init x y z))
(defn to-string [o]
(string-join @"Vector3(" (str (V3.x o)) @", " (str (V3.y o))
@", " (str (V3.z o)) @")"))
(string-join @"Vector3(" (str @(V3.x o)) @", " (str @(V3.y o))
@", " (str @(V3.z o)) @")"))
(defn zero []
(V3.init 0.0 0.0 0.0))
@ -117,37 +117,37 @@
(V3.init (random-between 0.0 1.0) (random-between 0.0 1.0) (random-between 0.0 1.0)))
(defn = [a b]
(and (Double.= (V3.x a) (V3.x b))
(and (Double.= (V3.y a) (V3.y b))
(Double.= (V3.z a) (V3.z b)))))
(and (Double.= @(V3.x a) @(V3.x b))
(and (Double.= @(V3.y a) @(V3.y b))
(Double.= @(V3.z a) @(V3.z b)))))
(defn /= [a b]
(not (= a b)))
(defn add [a b]
(V3.init (+ (V3.x a) (V3.x b))
(+ (V3.y a) (V3.y b))
(+ (V3.z a) (V3.z b))))
(V3.init (+ @(V3.x a) @(V3.x b))
(+ @(V3.y a) @(V3.y b))
(+ @(V3.z a) @(V3.z b))))
(defn sub [a b]
(V3.init (- (V3.x a) (V3.x b))
(- (V3.y a) (V3.y b))
(- (V3.z a) (V3.z b))))
(V3.init (- @(V3.x a) @(V3.x b))
(- @(V3.y a) @(V3.y b))
(- @(V3.z a) @(V3.z b))))
(defn mul [a n]
(V3.init (* (V3.x a) n)
(* (V3.y a) n)
(* (V3.z a) n)))
(V3.init (* @(V3.x a) n)
(* @(V3.y a) n)
(* @(V3.z a) n)))
(defn div [a n]
(V3.init (/ (V3.x a) n)
(/ (V3.y a) n)
(/ (V3.z a) n)))
(V3.init (/ @(V3.x a) n)
(/ @(V3.y a) n)
(/ @(V3.z a) n)))
(defn mag-sq [o]
(let [x (V3.x o)
y (V3.y o)
z (V3.z o)]
(let [x @(V3.x o)
y @(V3.y o)
z @(V3.z o)]
(+ (* x x) (+ (* y y) (* z z)))))
(defn mag [o]
@ -161,17 +161,17 @@
(defn cross [x y]
(V3.init
(- (* (V3.y x) (V3.z y))
(* (V3.z x) (V3.y y)))
(- (* (V3.z x) (V3.x y))
(* (V3.x x) (V3.z y)))
(- (* (V3.x x) (V3.y y))
(* (V3.y x) (V3.x y)))))
(- (* @(V3.y x) @(V3.z y))
(* @(V3.z x) @(V3.y y)))
(- (* @(V3.z x) @(V3.x y))
(* @(V3.x x) @(V3.z y)))
(- (* @(V3.x x) @(V3.y y))
(* @(V3.y x) @(V3.x y)))))
(defn dot [x y]
(+ (* (V3.x x) (V3.x y))
(+ (* (V3.y x) (V3.y y))
(* (V3.z x) (V3.z y)))))
(+ (* @(V3.x x) @(V3.x y))
(+ (* @(V3.y x) @(V3.y y))
(* @(V3.z x) @(V3.z y)))))
(defn angle-between [a b]
(let [dmm (/ (dot a b) (* (mag a) (mag b)))]
@ -187,9 +187,9 @@
(= (angle-between a b) (/ Double.pi 2.0)))
(defn lerp [a b amnt]
(init (* (- (V3.x b) (V3.x a)) amnt)
(* (- (V3.y b) (V3.y a)) amnt)
(* (- (V3.z b) (V3.z a)) amnt)))
(init (* (- @(V3.x b) @(V3.x a)) amnt)
(* (- @(V3.y b) @(V3.y a)) amnt)
(* (- @(V3.z b) @(V3.z a)) amnt)))
)
(defmodule VectorN
@ -210,7 +210,7 @@
(defn to-string [o]
(string-join @"VectorN(dim=" (str (VN.n o)) @", vals=" (str (VN.v o))
(string-join @"VectorN(dim=" (str @(VN.n o)) @", vals=" (str (VN.v o))
@")"))
(defn zip- [f a b]
@ -221,14 +221,14 @@
(VN.init (Array.count a) total))))
(defn zip [f a b]
(if (= (VN.n a) (VN.n b))
(if (= @(VN.n a) @(VN.n b))
(zip- f (VN.v a) (VN.v b))
(do
(IO.println "Error: vectors are of wrong dimensionality")
(VN.copy a))))
(defn = [a b]
(and (Int.= (VN.n a) (VN.n b))
(and (Int.= @(VN.n a) @(VN.n b))
(Array.= (VN.v a) (VN.v b))))
(defn /= [a b]
@ -241,10 +241,10 @@
(zip - a b))
(defn mul [a n]
(zip- * (VN.v a) &(Array.replicate (VN.n a) &n)))
(zip- * (VN.v a) &(Array.replicate @(VN.n a) &n)))
(defn div [a n]
(zip- / (VN.v a) &(Array.replicate (VN.n a) &n)))
(zip- / (VN.v a) &(Array.replicate @(VN.n a) &n)))
(defn square- [n]
(* @n @n))
@ -285,6 +285,6 @@
(= (angle-between a b) (/ Double.pi 2.0)))
(defn lerp [a b amnt]
(init (VN.n a) @(VN.v &(zip- * &(Array.replicate (VN.n a) &amnt)
(init @(VN.n a) @(VN.v &(zip- * &(Array.replicate @(VN.n a) &amnt)
(VN.v &(zip - b a))))))
)

View File

@ -91,7 +91,7 @@
(defn updating []
(let [p1 (Peep.init 9999 (String.copy "jaha") (A.init (String.copy "mmm")))
p2 (Peep.update-x p1 inc)]
(println (ref (str (Peep.x (ref p2)))))))
(println (ref (str @(Peep.x (ref p2)))))))
(defn character []
(println (ref (Char.str \#))))

View File

@ -11,7 +11,7 @@
(deftype Box [x Int])
(defmodule Box
(defn fmap [f box] (let [new-x (f (Box.x &box))]
(defn fmap [f box] (let [new-x (f @(Box.x &box))]
(Box.set-x box new-x))))
(use Box)
@ -21,8 +21,8 @@
(defn main []
(do
(println &(str (Box.x &(fmap inc (Box.init 100)))))
(println &(str (Box.x &(Box.fmap inc (Box.init 100)))))
(println &(str @(Box.x &(fmap inc (Box.init 100)))))
(println &(str @(Box.x &(Box.fmap inc (Box.init 100)))))
(println &(str &(ArrayExtension.fmap inc [10 20 30 40 50])))
(println &(str &(fmap inc [10 20 30 40 50])))
(println &(Array.str &(fmap inc [10 20 30 40 50])))

View File

@ -54,7 +54,7 @@
(let [lines (random-lines)
n (count &lines)]
(SDL_RenderDrawLines rend (raw lines) n))
(let [img (Images.img1 (the (Ref Images) images))]
(let [img @(Images.img1 (the (Ref Images) images))]
(SDL_RenderCopyEx rend
img
(address (dimensions img))

View File

@ -8,5 +8,5 @@
(IO.println &(str &(Pair.init [1 2 3] [true false true false])))
(IO.println &(str &(Pair.update-x (Pair.init 100 100) Int.inc)))
(IO.println &(str &(Pair.set-y (Pair.init 100 100) 200)))
(IO.println &(str (Pair.x &(Pair.init 100 100))))
(IO.println &(str @(Pair.x &(Pair.init 100 100))))
))

View File

@ -2,13 +2,14 @@
(deftype Age [x Int])
;; TODO! Should be possible to use the IntRef implementation of = and < here but it resolves to the = in the Age module...
(defmodule Age
(defn = [a b]
(Int.= (Age.x a) (Age.x b)))
(Int.= @(Age.x a) @(Age.x b)))
(defn > [a b]
(Int.> (Age.x a) (Age.x b)))
(Int.> @(Age.x a) @(Age.x b)))
(defn < [a b]
(Int.< (Age.x a) (Age.x b))))
(Int.< @(Age.x a) @(Age.x b))))
(defn main []
(let-do [ints (sort [10 3 75 40])

View File

@ -1,5 +1,5 @@
(load "Debug.carp")
;; (Debug.sanitize-addresses)
(Debug.sanitize-addresses)
;; (project-set! "printAST" "true")
(local-include "core.h")
(project-set! "echoCompilationCommand" "true")
@ -42,18 +42,37 @@
;; (defn main [] (println* &(f)))
;; Generic Hashmap Stuff
;; (use Array)
(use Array)
(deftype (Entry a b) [key a value b])
(deftype (Bucket a b) [length Int, entries (Array (Entry a b))])
(info Bucket)
(deftype (Bucket a b) [entries (Array (Entry a b))])
(defmodule String
(defn zero [] @""))
(defn get-object [bucket lookup-key]
(let-do [pairs (Bucket.entries bucket)
result (zero)]
(for [i 0 (count pairs)]
(let [pair (nth pairs i)]
(when (= (Entry.key pair) &lookup-key)
(set! &result @(Entry.value pair)))))
result))
(defn main []
(let-do [start (Bucket.init [(Entry.init @"hello" 12345)
(Entry.init @"goodbye" 666)])]
(IO.println &(str &start))
(IO.println &(Int.str (get-object &start @"hello")))
))
;; ;;(defn main [] (println* &(Bucket.init 10000 [(Entry.init @"hello" 12345) (Entry.init @"goodbye" 666)])))
;; (defn f [x] (Int.+ (Bucket.length &x)))
;; ;; (defn empty []
;; ;; (Bucket.init 0 []))
;; ;; (defn f []
;; ;; (the (Bucket Float Bool) (empty))) ;; run checks in the concretizer to fix this

View File

@ -142,9 +142,8 @@ templatesForSingleMember :: TypeEnv -> Env -> [String] -> Ty -> (XObj, XObj) ->
templatesForSingleMember typeEnv env insidePath p@(StructTy typeName _) (nameXObj, typeXObj) =
let Just t = xobjToTy typeXObj
memberName = getName nameXObj
fixedMemberTy = if isManaged typeEnv t then RefTy t else t
in [instanceBinderWithDeps (SymPath insidePath memberName) (FuncTy [RefTy p] fixedMemberTy) (templateGetter (mangle memberName) fixedMemberTy)
, if typeIsGeneric fixedMemberTy
in [instanceBinderWithDeps (SymPath insidePath memberName) (FuncTy [RefTy p] (RefTy t)) (templateGetter (mangle memberName) (RefTy t))
, if typeIsGeneric t
then (templateGenericSetter insidePath p t memberName, [])
else instanceBinderWithDeps (SymPath insidePath ("set-" ++ memberName)) (FuncTy [p, t] p) (templateSetter typeEnv env (mangle memberName) t)
,instanceBinderWithDeps (SymPath insidePath ("set-" ++ memberName ++ "!")) (FuncTy [RefTy (p), t] UnitTy) (templateSetterRef typeEnv env (mangle memberName) t)
@ -336,14 +335,10 @@ memberAssignment allocationMode (memberName, _) = " instance" ++ sep ++ membe
-- | The template for getters of a deftype.
templateGetter :: String -> Ty -> Template
templateGetter member fixedMemberTy =
let maybeAmpersand = case fixedMemberTy of
RefTy _ -> "&"
_ -> ""
in
Template
(FuncTy [RefTy (VarTy "p")] (VarTy "t"))
(const (toTemplate "$t $NAME($(Ref p) p)"))
(const (toTemplate ("$DECL { return " ++ maybeAmpersand ++ "(p->" ++ member ++ "); }\n")))
(const (toTemplate ("$DECL { return &(p->" ++ member ++ "); }\n")))
(const [])
-- | The template for setters of a deftype.

View File

@ -19,9 +19,9 @@
(defmodule Young
(defn = [y1 y2]
(Int.= (age y1) (age y2)))
(Int.= @(age y1) @(age y2)))
(defn /= [y1 y2]
(Int.= (age y1) (age y2))))
(Int.= @(age y1) @(age y2))))
;; Now I want to use 'Young.=' in with 'Ur.compare'
(defn f []

View File

@ -109,6 +109,6 @@
Double.approx)
(assert-equal test
2.0
(Summary.median &(summary &[1.0 2.0 3.0]))
@(Summary.median &(summary &[1.0 2.0 3.0]))
"summary works as expected")
(print-test-results test)))