Skip to content

Commit c28b257

Browse files
committed
Add ability to manipulate scheme objects
This is step 0 in a plan to use the scheme evaluator to evaluate Idris expressions at compile time. As a proof of concept, I've got this working for a toy language here: https://github.com/edwinb/SchemeEval We won't be able to do anything interesting with this in Idris itself until the next release because it involves updating the bootstrap code and adding the ability to pass 'Integer' to foreign calls, which really should have been allowed anyway since it's for a backend to decide what it can cope with, not Idris itself.
1 parent 328617d commit c28b257

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

src/Compiler/CompileExpr.idr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ data NArgs : Type where
575575
NPtr : NArgs
576576
NGCPtr : NArgs
577577
NBuffer : NArgs
578+
NForeignObj : NArgs
578579
NIORes : Closure [] -> NArgs
579580

580581
getPArgs : {auto c : Ref Ctxt Defs} ->
@@ -611,6 +612,7 @@ getNArgs defs (NS _ (UN "AnyPtr")) [] = pure NPtr
611612
getNArgs defs (NS _ (UN "GCPtr")) [arg] = pure NGCPtr
612613
getNArgs defs (NS _ (UN "GCAnyPtr")) [] = pure NGCPtr
613614
getNArgs defs (NS _ (UN "Buffer")) [] = pure NBuffer
615+
getNArgs defs (NS _ (UN "ForeignObj")) [] = pure NForeignObj
614616
getNArgs defs (NS _ (UN "Unit")) [] = pure NUnit
615617
getNArgs defs (NS _ (UN "Struct")) [n, args]
616618
= do NPrimVal _ (Str n') <- evalClosure defs n
@@ -621,6 +623,7 @@ getNArgs defs n args = pure $ User n args
621623
nfToCFType : {auto c : Ref Ctxt Defs} ->
622624
FC -> (inStruct : Bool) -> NF [] -> Core CFType
623625
nfToCFType _ _ (NPrimVal _ IntType) = pure CFInt
626+
nfToCFType _ _ (NPrimVal _ IntegerType) = pure CFInteger
624627
nfToCFType _ _ (NPrimVal _ Bits8Type) = pure CFUnsigned8
625628
nfToCFType _ _ (NPrimVal _ Bits16Type) = pure CFUnsigned16
626629
nfToCFType _ _ (NPrimVal _ Bits32Type) = pure CFUnsigned32
@@ -662,6 +665,7 @@ nfToCFType _ s (NTCon fc n_in _ _ args)
662665
NPtr => pure CFPtr
663666
NGCPtr => pure CFGCPtr
664667
NBuffer => pure CFBuffer
668+
NForeignObj => pure CFForeignObj
665669
NIORes uarg =>
666670
do narg <- evalClosure defs uarg
667671
carg <- nfToCFType fc s narg

src/Core/CompileExpr.idr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public export
153153
data CFType : Type where
154154
CFUnit : CFType
155155
CFInt : CFType
156+
CFInteger : CFType
156157
CFInt8 : CFType
157158
CFInt16 : CFType
158159
CFInt32 : CFType
@@ -167,6 +168,7 @@ data CFType : Type where
167168
CFPtr : CFType
168169
CFGCPtr : CFType
169170
CFBuffer : CFType
171+
CFForeignObj : CFType
170172
CFWorld : CFType
171173
CFFun : CFType -> CFType -> CFType
172174
CFIORes : CFType -> CFType
@@ -350,6 +352,7 @@ export
350352
Show CFType where
351353
show CFUnit = "Unit"
352354
show CFInt = "Int"
355+
show CFInteger = "Integer"
353356
show CFInt8 = "Int_8"
354357
show CFInt16 = "Int_16"
355358
show CFInt32 = "Int_32"
@@ -364,6 +367,7 @@ Show CFType where
364367
show CFPtr = "Ptr"
365368
show CFGCPtr = "GCPtr"
366369
show CFBuffer = "Buffer"
370+
show CFForeignObj = "ForeignObj"
367371
show CFWorld = "%World"
368372
show (CFFun s t) = show s ++ " -> " ++ show t
369373
show (CFIORes t) = "IORes " ++ show t

src/Core/Hash.idr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ Hashable CFType where
235235
h `hashWithSalt` 19
236236
CFInt64 =>
237237
h `hashWithSalt` 20
238+
CFForeignObj =>
239+
h `hashWithSalt` 21
240+
CFInteger =>
241+
h `hashWithSalt` 22
238242

239243
export
240244
Hashable Constant where

src/Core/TTC.idr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@ TTC CFType where
785785
toBuf b CFInt16 = tag 18
786786
toBuf b CFInt32 = tag 19
787787
toBuf b CFInt64 = tag 20
788+
toBuf b CFForeignObj = tag 21
789+
toBuf b CFInteger = tag 22
788790

789791
fromBuf b
790792
= case !getTag of
@@ -809,6 +811,8 @@ TTC CFType where
809811
18 => pure CFInt16
810812
19 => pure CFInt32
811813
20 => pure CFInt64
814+
21 => pure CFForeignObj
815+
22 => pure CFInteger
812816
_ => corrupt "CFType"
813817

814818
export

support/chez/support.ss

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,70 @@
481481
(when x
482482
(((cdr x) (car x)) 'erased)
483483
(run))))))
484+
485+
;; For creating and reading back scheme objects
486+
487+
; read a scheme string and evaluate it, returning 'Just result' on success
488+
; TODO: catch exception!
489+
(define (blodwen-eval-scheme str)
490+
(box (eval (read (open-input-string str))))) ; box == Just
491+
492+
(define (blodwen-eval-okay obj)
493+
(if (null? obj)
494+
0
495+
1))
496+
497+
(define (blodwen-get-eval-result obj)
498+
(unbox obj))
499+
500+
(define (blodwen-debug-scheme obj)
501+
(display obj) (newline))
502+
503+
(define (blodwen-is-number obj)
504+
(if (number? obj) 1 0))
505+
506+
(define (blodwen-is-integer obj)
507+
(if (integer? obj) 1 0))
508+
509+
(define (blodwen-is-float obj)
510+
(if (flonum? obj) 1 0))
511+
512+
(define (blodwen-is-char obj)
513+
(if (char? obj) 1 0))
514+
515+
(define (blodwen-is-string obj)
516+
(if (string? obj) 1 0))
517+
518+
(define (blodwen-is-procedure obj)
519+
(if (procedure? obj) 1 0))
520+
521+
(define (blodwen-is-symbol obj)
522+
(if (symbol? obj) 1 0))
523+
524+
(define (blodwen-is-vector obj)
525+
(if (vector? obj) 1 0))
526+
527+
(define (blodwen-is-nil obj)
528+
(if (null? obj) 1 0))
529+
530+
(define (blodwen-is-pair obj)
531+
(if (pair? obj) 1 0))
532+
533+
(define (blodwen-make-symbol str)
534+
(string->symbol str))
535+
536+
; The below rely on checking that the objects are the right type first.
537+
538+
(define (blodwen-vector-ref obj i)
539+
(vector-ref obj i))
540+
541+
(define (blodwen-vector-length obj)
542+
(vector-length obj))
543+
544+
(define (blodwen-apply obj arg)
545+
(obj arg))
546+
547+
(define (blodwen-read-symbol sym)
548+
(symbol->string sym))
549+
550+
(define (blodwen-id x) x)

0 commit comments

Comments
 (0)