Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[WIP] Compile Linklets to Javascript #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 176 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
176 commits
Select commit Hold shift + click to select a range
ff4657b
WIP
gamburgm Aug 28, 2021
e64661c
wip
gamburgm Sep 26, 2021
2ec4cf4
parsing and expanding success
gamburgm Sep 28, 2021
d2aa0d6
expand linklets!
gamburgm Oct 26, 2021
a36b41f
transform linklet AST to IR
gamburgm Nov 1, 2021
e5fc2b8
add arity checks & assemble
gamburgm Nov 2, 2021
866b464
linklets run
gamburgm Nov 12, 2021
025a443
build output directory and compile runtime imports
gamburgm Dec 13, 2021
8a965a8
linklets directory
gamburgm Dec 29, 2021
bd1b071
linklets directory doesn't matter, regenerate expander linklet yourself
gamburgm Dec 29, 2021
5066796
cover unsafe forms that weren't provided by kernel runtime
gamburgm Jan 4, 2022
939490a
do not commit
gamburgm Jan 4, 2022
023f6c7
Merge branch 'master' into gamburgm/compile-linklet-to-js
gamburgm Jan 10, 2022
264eeeb
Merge branch 'master' into gamburgm/compile-linklet-to-js
gamburgm Jan 14, 2022
441cc38
ephemeron hashes
gamburgm Jan 24, 2022
c93292b
expander linklet for convenience
gamburgm Jan 27, 2022
0b77e8b
linklet shim wip
gamburgm Mar 10, 2022
fa84357
start of linklet api
gamburgm Mar 11, 2022
d748f8c
to view
gamburgm Mar 17, 2022
bcb64f4
use the specified number in the displayln form you parse
gamburgm Mar 30, 2022
7c44df3
merge in previous impl
gamburgm Mar 30, 2022
ecd46ed
bugfixes, now works on baby examples
gamburgm Mar 31, 2022
2d4092c
port Sam TH's PR into mine
gamburgm Apr 2, 2022
926262c
wip on formals
gamburgm Apr 9, 2022
30c18a4
generate expander successfully
gamburgm Apr 11, 2022
8d97dff
preparing for generation of primitive table(s)
gamburgm Apr 11, 2022
1b8bae1
primitive table wip
gamburgm Apr 12, 2022
38e635d
skeleton for primitive table
gamburgm Apr 12, 2022
ce0f9e1
linklet primitive table
gamburgm Apr 17, 2022
0e22f37
added a primitive table
gamburgm Apr 19, 2022
7d66e6e
add some more linklet primitives
gamburgm Apr 19, 2022
9807f2c
things are not going according to plan
gamburgm Apr 26, 2022
d4744a3
add hook to stdlib exports adding those forms to the kernel primitive…
gamburgm Apr 26, 2022
8194886
add back the unsafe forms, I'll handle imports and exports later
gamburgm Apr 26, 2022
6148ab3
made more progress
gamburgm Apr 27, 2022
f6a6cfb
get back to core forms
gamburgm May 19, 2022
60461a6
two forms
gamburgm Jun 17, 2022
be7111d
Merge branch 'master' into gamburgm/compile-linklet-to-js
gamburgm Jun 17, 2022
5d0c226
implement kernel table as hash plus more core forms
gamburgm Jun 19, 2022
e116e58
add support for paramz table
gamburgm Jun 20, 2022
1555825
add missing tables as empty
gamburgm Jun 20, 2022
0cc4250
add parameter, no errors on execution
gamburgm Jun 24, 2022
8dfe873
export forms, attempt to use them
gamburgm Jun 24, 2022
ac7c9e7
source locations
gamburgm Jun 26, 2022
8277426
add simple thread runtime
gamburgm Jun 28, 2022
c72b29b
semaphores
gamburgm Jun 28, 2022
1627812
corrected dynamic-wind semantics
gamburgm Jun 29, 2022
05626f0
call-with-values bug took two days
gamburgm Jun 29, 2022
742cd70
remove output port from codegen module
gamburgm Jul 6, 2022
d5fb1c3
more forms
gamburgm Jul 6, 2022
b525f99
fprintf add forms and add kernel parameter
gamburgm Jul 6, 2022
e2b92d1
awful hack to get exceptions to work and print a stacktrace
gamburgm Jul 6, 2022
9e6daa9
fine
gamburgm Jul 12, 2022
1ff92c4
Merge branch 'gamburgm/compile-linklet-to-js' of github.com:gamburgm/…
gamburgm Jul 12, 2022
da642e3
expanding 5 worksgit add random.js!
gamburgm Jul 12, 2022
4c945db
I can compile 5+5
gamburgm Jul 12, 2022
f46a7e3
WIP bugs
gamburgm Jul 12, 2022
2bff959
wip
gamburgm Jul 12, 2022
2a04740
fix for backwards compatibility reasons
gamburgm Jul 13, 2022
e623b14
list-tail
gamburgm Jul 13, 2022
a6610b5
update runtime
gamburgm Jul 13, 2022
9ba69db
bugs for strings/printing?
gamburgm Jul 14, 2022
90bf051
fix list-tail and printing for linklets
gamburgm Jul 19, 2022
6b16867
update bootstrapper
gamburgm Jul 19, 2022
09de7d3
more forms
gamburgm Jul 21, 2022
146f58b
update shim
gamburgm Jul 21, 2022
c8224f9
start linklet api
gamburgm Jul 21, 2022
98ef6ba
more decent start
gamburgm Jul 21, 2022
4f80b22
rename identifier
gamburgm Jul 21, 2022
5985f5c
new no-contract AST, WIP
gamburgm Jul 22, 2022
f2819a2
implement most of AST without contracts
gamburgm Jul 22, 2022
c089775
clone linklet expander and start porting it
gamburgm Jul 22, 2022
b6d864d
export forms
gamburgm Jul 26, 2022
b3038ca
get rid of dependency on absyn.rkt
gamburgm Jul 26, 2022
cedb869
Merge branch 'gamburgm/compile-linklet-to-js' of github.com:gamburgm/…
gamburgm Jul 26, 2022
bae48bc
update some imports/exports
gamburgm Jul 26, 2022
70d3f4d
WIP
gamburgm Jul 27, 2022
2d44a28
add basic s-expression pattern matcher based on schemify's
gamburgm Jul 29, 2022
969fe8a
update matcher
gamburgm Jul 29, 2022
febc305
use new match construct
gamburgm Jul 29, 2022
9c3244a
check success
gamburgm Jul 29, 2022
7e564fb
really dumb struct-match form
gamburgm Jul 29, 2022
680f7c0
define in-line definition form
gamburgm Jul 29, 2022
14d626a
type-less struct forms WIP
gamburgm Jul 30, 2022
fe34533
transform pass WIP
gamburgm Jul 30, 2022
7afd8c7
rough complete new structs
gamburgm Jul 30, 2022
bc13725
new syntax-case logger
gamburgm Jul 30, 2022
9df7b45
really dumb sets since I can't use them without racket/set
gamburgm Jul 30, 2022
4443bd7
re-write config.rkt without types and without dependencies
gamburgm Jul 30, 2022
7d11cc4
add Provide forms
gamburgm Jul 30, 2022
6fc794b
remove more types
gamburgm Jul 30, 2022
c25f834
add match-define
gamburgm Jul 30, 2022
9c89208
more transform updates
gamburgm Jul 30, 2022
94b7b06
stripping types from ident wip
gamburgm Jul 31, 2022
2be8454
getting rid of types from util wip
gamburgm Jul 31, 2022
fb431ca
add guard with pattern variables in scope
gamburgm Jul 31, 2022
c9ac1e0
some more set methods
gamburgm Jul 31, 2022
2f9823e
types fully removed from ident.rkt
gamburgm Jul 31, 2022
3b6c49b
reinclude tests with rackunit require only in test submodule
gamburgm Jul 31, 2022
0e09433
fixing some requires in util.rkt, WIP
gamburgm Jul 31, 2022
37c2ae1
get rid of unusable requires
gamburgm Jul 31, 2022
bc32606
get rid of let/cc, racketscript can't handle that
gamburgm Jul 31, 2022
03aec95
remove one no-longer-imported function
gamburgm Jul 31, 2022
362885d
replacement for links function, pretty shoddy but hopefully works
gamburgm Jul 31, 2022
e023b38
bugs, from manual testing seems equivalent
gamburgm Jul 31, 2022
33fa275
add set-map
gamburgm Aug 1, 2022
f20b994
getting rid of more types
gamburgm Aug 1, 2022
09fcdf9
move some anaphoric macros into repo so I don't need the external dep…
gamburgm Aug 1, 2022
eb386d0
types removed from util.rkt
gamburgm Aug 1, 2022
68d038f
add set->list
gamburgm Aug 1, 2022
4dbc4d9
add format-symbol from racket/syntax
gamburgm Aug 1, 2022
26454c2
removing types and dependencies WIP
gamburgm Aug 1, 2022
ab95cf5
remove types from environment
gamburgm Aug 1, 2022
bdef13e
more type removal WIP
gamburgm Aug 1, 2022
9b6d3f8
add missing structs
gamburgm Aug 1, 2022
d31326a
add match-lambda forms
gamburgm Aug 1, 2022
f2df8d5
add support for top-level underscore and identifier patterns
gamburgm Aug 3, 2022
1fcbd94
remove diagnostics
gamburgm Aug 3, 2022
59c5362
generate correct predicate for structs
gamburgm Aug 3, 2022
68d14c2
add predicates for union types in AST
gamburgm Aug 3, 2022
2ec0356
more complete support for predicate forms in struct-match patterns
gamburgm Aug 3, 2022
2d07519
bugs in predicate pattern codegen
gamburgm Aug 3, 2022
e817dd7
check patterns at compile time and add guards to struct-match
gamburgm Aug 4, 2022
0ca4945
wrong signature for ILExnHandler
gamburgm Aug 4, 2022
95bf528
remove diagnostics
gamburgm Aug 4, 2022
f000d6a
more struct-match bugs
gamburgm Aug 4, 2022
5fd3886
fix set impl
gamburgm Aug 4, 2022
b2eeb81
correct structs for ILIf, etc
gamburgm Aug 4, 2022
9e8db34
il-analyze changes wip
gamburgm Aug 4, 2022
428944a
remove guard from code gen
gamburgm Aug 4, 2022
83f8a0d
add helper functions
gamburgm Aug 4, 2022
a4882ff
add set-union
gamburgm Aug 4, 2022
0bae33c
fix begin/let-nil issue
gamburgm Aug 4, 2022
de57c3b
wip
gamburgm Aug 4, 2022
b448d9e
match-let
gamburgm Aug 4, 2022
d10a1eb
set-remove and set-subtract
gamburgm Aug 4, 2022
f558670
rough draft of il-analyze impl
gamburgm Aug 4, 2022
0200ecb
struct-match-let
gamburgm Aug 5, 2022
3ec060b
il-analyze complete and tests pass
gamburgm Aug 5, 2022
841d85a
change imports in transform pass
gamburgm Aug 5, 2022
4637292
add predicates from typed ast
gamburgm Aug 5, 2022
3b344d5
macro-generating macro is unnecessary
gamburgm Aug 5, 2022
f91be2b
add guards to _ patterns and use '(let () ...)' over 'begin'
gamburgm Aug 5, 2022
34cf2f3
transform wip
gamburgm Aug 5, 2022
fdd6e2f
more forms
gamburgm Aug 5, 2022
192bb34
forms and imports for IR
gamburgm Aug 5, 2022
84800fe
once again, (let () ...) over begin
gamburgm Aug 5, 2022
6086e8a
add sequence-length to utils
gamburgm Aug 5, 2022
4d54576
done with transform.rkt
gamburgm Aug 5, 2022
816af6f
missed a few things
gamburgm Aug 5, 2022
96951e8
bug fixes, get rid of begin
gamburgm Aug 5, 2022
4696bf6
no dependencies!
gamburgm Aug 5, 2022
d5499de
verson/utils not allowed
gamburgm Aug 5, 2022
d9ed2db
delete some module-seeking forms, introducing more dead code
gamburgm Aug 6, 2022
c6591a2
stdlib missing and impl
gamburgm Aug 8, 2022
1ece969
stripped assembler, missing import support
gamburgm Aug 8, 2022
d21aa85
wip
gamburgm Aug 11, 2022
1797503
really dumb imports implementation
gamburgm Aug 11, 2022
dddefb4
racketscript linklet wow
gamburgm Aug 11, 2022
cb0dfc4
fix some bugs
gamburgm Aug 11, 2022
ee5c07d
adding support for more filesystem operations
gamburgm Aug 12, 2022
8e9ea63
use XOR
gamburgm Aug 12, 2022
5637b63
split-path
gamburgm Aug 12, 2022
38bc908
ridiculous script to help with compiling runtime
gamburgm Aug 12, 2022
16353a7
wip
gamburgm Aug 16, 2022
3c31200
bash instead of sh
gamburgm Aug 16, 2022
5589023
more runtime forms for racketscript linklet
gamburgm Aug 16, 2022
b1dc24c
new linklet
gamburgm Aug 16, 2022
73c51a2
include javascript linklet api
gamburgm Aug 16, 2022
b71af49
update runtime
gamburgm Aug 16, 2022
1710f84
rename random.js
gamburgm Aug 16, 2022
c51d3f4
equality + undoing circular dependency
gamburgm Aug 16, 2022
1947bea
capture more behavior with script
gamburgm Aug 17, 2022
199e65a
try to directly overwrite the linklet primitive table -- doesn't work
gamburgm Aug 17, 2022
c4c4471
handle primitive-table differently, to bootstrap linklets
gamburgm Aug 18, 2022
0654457
failed attempt to bootstrap via primitive-table
gamburgm Aug 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ TAGS

*.elc
node_modules/
*.rktl
!expander.rktl
!racketscript.rktl
88,894 changes: 88,894 additions & 0 deletions expander.rktl

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions flash-runtime.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

rm -rf ~/rs-orig/racketscript-compiler/racketscript/compiler/runtime/
rm -rf js-build/

cp -r racketscript-compiler/racketscript/compiler/runtime/ ~/rs-orig/racketscript-compiler/racketscript/compiler/runtime/

pushd ~/rs-orig/
./racketscript-compiler/bin/racks foobar.rkt

cp -r js-build/ ~/racketscript/js-build/

popd
58 changes: 58 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as Expander from './expander.js';
import * as CORE from '../runtime/core.js';
import { displayln as print } from '../runtime/kernel.rkt.js';

const demoNamespace = Expander.make_namespace();
const KERNEL_IMPORT = CORE.Pair.makeList(
CORE.PrimitiveSymbol.make('quote'),
CORE.PrimitiveSymbol.make('#%kernel')
);

const STX_KERNEL_IMPORT = CORE.Pair.makeList(
CORE.PrimitiveSymbol.make('for-syntax'),
CORE.Pair.makeList(
CORE.PrimitiveSymbol.make('quote'),
CORE.PrimitiveSymbol.make('#%kernel')
)
);

const SUM = CORE.Pair.makeList(CORE.PrimitiveSymbol.make('+'), 5, 5);

const LET_EX = CORE.Pair.makeList(
CORE.PrimitiveSymbol.make('let'),
CORE.Pair.makeList(CORE.Pair.makeList(CORE.PrimitiveSymbol.make('x'), 5)),
CORE.PrimitiveSymbol.make('x')
);

const DEFINE_EX = CORE.Pair.makeList(
CORE.PrimitiveSymbol.make('let'),
CORE.Pair.makeList(),
CORE.Pair.makeList(
CORE.PrimitiveSymbol.make('define'),
CORE.PrimitiveSymbol.make('x'),
5
),
CORE.Pair.makeList(
CORE.PrimitiveSymbol.make('set!'),
CORE.PrimitiveSymbol.make('x'),
6
),
CORE.PrimitiveSymbol.make('x')
);

Expander.namespace_attach_module(Expander.current_namespace(), KERNEL_IMPORT, demoNamespace);

Expander.namespace_require(KERNEL_IMPORT, demoNamespace);
Expander.namespace_require(STX_KERNEL_IMPORT, demoNamespace);

const expandExpr = (e) => {
const stxObj = Expander.namespace_syntax_introduce(Expander.datum__gt_syntax(false, e), demoNamespace);
return Expander.expand(stxObj, demoNamespace);
};

const compileExpr = (e) => {
expandExpr(e);
return Expander.compile(e, demoNamespace);
};

print(compileExpr(DEFINE_EX));
5 changes: 5 additions & 0 deletions notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
To run the expander:

1. racks -l expander.rktl
2. mv foo.js js-build/modules/
3. node js-build/modules/foo.js
11 changes: 10 additions & 1 deletion racketscript-compiler/racketscript/compiler/absyn.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

(provide (all-defined-out))

(struct Linklet ([path : Path-String] ;; TODO eventually get rid of paths, need it for now
[imports : (Listof (Listof (U Symbol (Listof Symbol))))]
[exports : (Listof (U Symbol (Listof Symbol)))]
[forms : (Listof GeneralTopLevelForm)])
#:transparent)

(struct Module ([id : Symbol]
[path : Path]
[lang : (U Symbol String (Listof Symbol))]
Expand All @@ -24,6 +30,7 @@
[TopLevelForm GeneralTopLevelForm
Expr
Module
Linklet
Begin]

[GeneralTopLevelForm Expr
Expand Down Expand Up @@ -84,7 +91,9 @@

[Ident (LocalIdent [id : Symbol])
(ImportedIdent [id : Symbol] [src-mod : Module-Path] [reachable? : Boolean])
(TopLevelIdent [id : Symbol])]
(TopLevelIdent [id : Symbol])
(PrimitiveIdent [id : Symbol])
(LinkletImportIdent [id : Symbol] [position : Integer])]

[Begin (Listof TopLevelForm)]

Expand Down
29 changes: 29 additions & 0 deletions racketscript-compiler/racketscript/compiler/anaphoric.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#lang racket/base

;; take some of the forms from the anaphoric library, without including it as a dependency
;; for the project.
;;
;; Taken or modified from: https://github.com/SuzanneSoy/anaphoric

(require racket/stxparam
(for-syntax racket/base))

(provide it acond)

(define-syntax-parameter it
(λ (stx)
(raise-syntax-error
'it
"Use of the \"it\" identifier is only allowed within anaphoric macros."
stx)))

(define-syntax (acond stx)
(syntax-case stx (else)
[(_ [else . else-body]) #'(begin . else-body)]
[(_) #'(void)]
[(_ [condition . body] . rest)
#'(let ([tmp condition])
(if tmp
(syntax-parameterize ([it (make-rename-transformer #'tmp)])
. body)
(acond . rest)))]))
126 changes: 67 additions & 59 deletions racketscript-compiler/racketscript/compiler/assembler.rkt
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
#lang typed/racket/base
#lang racket/base

;;; Generate JavaScript code from abstract syntax. Each binding name
;;; in assumed to be fresh, to enforce lexical scope rules of Racket
;;; in JavaScript

(require racket/format
racket/function
racket/list
racket/match
(require racket/list
racket/string
(only-in racket/function curry)
"match.rkt"
"struct-match.rkt"
"config.rkt"
"il.rkt"
"ir.rkt"
"logging.rkt"
"util.rkt")

(provide assemble
assemble-module
assemble-linklet
assemble-statement*
assemble-statement)

(: assemble (-> ILProgram Void))
(define (assemble p)
(assemble-statement* p (current-output-port)))

(: assemble-expr (-> ILExpr Output-Port Void))
(define (assemble-expr expr out)
(define emit (curry fprintf out))
(: emit-args (-> (Listof ILExpr) String Void))

(define (emit-args args sep)
(let loop ([a* args])
(match a*
['() (void)]
[(cons a '()) (assemble-expr a out)]
[(cons a tl)
[`() (void)]
[`(,a) (assemble-expr a out)]
[`(,a . ,tl)
(assemble-expr a out)
(emit sep)
(loop tl)])))

(: wrap-e? (-> ILExpr Boolean))
;; Wrap expression 'e' when being applied, referenced
;; or indexed
(define (wrap-e? e)
Expand All @@ -49,16 +46,16 @@
(number? (ILValue-v e))
(byte? (ILValue-v e)))))

(match expr
(struct-match expr
[(ILLambda args body)
(emit "function(")
(define args-str : (Listof String)
(define args-str
(cond
[(symbol? args) (list (~a "..." (normalize-symbol args)))]
[(list? args) (map normalize-symbol args)]
[(cons? args) (append1 (map normalize-symbol (car args))
(~a "..." (normalize-symbol (cdr args))))]
[else (error 'assemble-expr "λ must be unchecked by assembler phase")]))
[else (displayln args) (error 'assemble-expr "λ must be unchecked by assembler phase")]))
(emit (string-join args-str ", "))
(emit ") {")
(for ([s body])
Expand All @@ -72,7 +69,7 @@
(emit-args args ",")
(emit ")")]
[(ILBinaryOp oper args)
(assert (>= (length args) 2)) ;; TODO: Update the type of ILBinaryOp
;; (assert (>= (length args) 2)) ;; TODO Update the type of ILBinaryOp
(for/last? ([arg last? args])
(when (ILBinaryOp? arg) (emit "("))
(assemble-expr arg out)
Expand Down Expand Up @@ -138,15 +135,13 @@
(emit (~a (normalize-symbol expr)))]
[_ (error "unsupported expr" (void))]))

(: assemble-statement* (-> ILStatement* Output-Port Void))
(define (assemble-statement* stmt* out)
(for [(s stmt*)]
(assemble-statement s out)))

(: assemble-statement (-> ILStatement Output-Port Void))
(define (assemble-statement stmt out)
(define emit (curry fprintf out))
(match stmt
(struct-match stmt
[(ILVarDec id expr)
(emit (~a "var " (normalize-symbol id)))
(when expr
Expand All @@ -173,12 +168,13 @@
(emit "}")]
[(ILIf* clauses)
(let loop ([clauses clauses])
(match clauses
[(cons (ILIfClause #f body) ctl)
(match-define `(,hd . ,ctl) clauses)
(struct-match hd
[(ILIfClause cnd body) #:when (not cnd)
(emit "{")
(assemble-statement* body out)
(emit "}")]
[(cons (ILIfClause (? ILExpr? pred) body) ctl)
[(ILIfClause pred body) #:when (ILExpr? pred)
(emit "if (")
(assemble-expr pred out)
(emit ") {")
Expand Down Expand Up @@ -234,35 +230,41 @@
(emit ")"))
(emit ";")]))

(: assemble-module (-> ILModule (Option Output-Port) Void))
(define (assemble-module mod maybeout)
(match-define (ILModule id provides requires body) mod)
(log-rjs-info "[assemble] ~a" id)
(let ([cb (λ ([out : Output-Port])
(assemble-requires* requires out)
(for ([b body])
(assemble-statement b out))
(assemble-provides* provides out))])
(if maybeout
(cb maybeout)
;; For all other cases we need a valid module name, eg. kernel
(call-with-output-file (module-output-file (assert id path?))
#:exists 'replace
cb))))

(: assemble-requires* (-> ILRequire* Output-Port Void))
(define (assemble-linklet lnk out)
(struct-match-define (ILLinklet imports exports body) lnk)
(log-rjs-info "[assemble linklet]")

(assemble-requires* imports out)
(for ([b body])
(assemble-statement b out))
(assemble-provides* exports out))

;; (define (assemble-module mod maybeout)
;; (match-define (ILModule id provides requires body) mod)
;; (log-rjs-info "[assemble] ~a" id)
;; (let ([cb (λ ([out : Output-Port])
;; (assemble-requires* requires out)
;; (for ([b body])
;; (assemble-statement b out))
;; (assemble-provides* provides out))])
;; (if maybeout
;; (cb maybeout)
;; ;; For all other cases we need a valid module name, eg. kernel
;; (call-with-output-file (module-output-file (assert id path?))
;; #:exists 'replace
;; cb))))

(define (assemble-requires* reqs* out)
(define emit (curry fprintf out))
(define core-import-path
;; (current-source-file) should not be false, else abort
(jsruntime-import-path (cast (current-source-file) (U Symbol Path))
(jsruntime-module-path 'core)))

;; need some relative mechanism to do this in the future

(emit "import * as ~a from '~a';"
(jsruntime-core-module)
core-import-path)
"../runtime/core.js")

(for ([req reqs*])
(match-define (ILRequire mod obj-name import-sym) req)
(struct-match-define (ILRequire mod obj-name import-sym) req)
(define import-string
(case import-sym
[(default) (format "import ~a from \"~a\";"
Expand All @@ -275,14 +277,21 @@

(emit import-string)))

(: assemble-provides* (-> ILProvide* Output-Port Void))
;; (define (assemble-requires* reqs* out)
;; (define emit (curry fprintf out))
;; (define core-import-path
;; ;; (current-source-file) should not be false, else abort
;; (jsruntime-import-path (cast (current-source-file) (U Symbol Path))
;; (jsruntime-module-path 'core)))


(define (assemble-provides* p* out)
(define emit (curry fprintf out))

(unless (empty? p*)
(emit "export { ")
(for/last? ([p last? p*])
(match p
(struct-match p
[(ILSimpleProvide id)
(emit (~a (normalize-symbol id)))]
[(ILRenamedProvide local-id exported-id)
Expand All @@ -294,28 +303,27 @@

(emit " };")))

(: assemble-value (-> Any Output-Port Void))
(define (assemble-value v out)
(define emit (curry fprintf out))
;; TODO: this will eventually be replaced by runtime primitives
;; TODO this will eventually be replaced by runtime primitives
(cond
[(string? v)
(emit "~s" v)]
[(number? v)
(match v
[+inf.0 (emit "Infinity")]
[-inf.0 (emit "-Infinity")]
[+nan.0 (emit "NaN")]
[+nan.f (emit "NaN")]
[_ #:when (single-flonum? v) (emit (~a (exact->inexact (inexact->exact v))))]
[_ (emit (~a v))])] ;; TODO
(cond
[(equal? +inf.0 v) (emit "Infinity")]
[(equal? -inf.0 v) (emit "-Infinity")]
[(equal? +nan.0 v) (emit "NaN")] ;; same as +nan.f
;; [(equal? +nan.f v) (emit "NaN")]
[(single-flonum? v) (emit (~a (exact->inexact (inexact->exact v))))]
[else (emit (~a v))])] ;; TODO
[(boolean? v) (emit (if v "true" "false"))]
[(void? v)
(emit "null")]
[else (error "Unexpected value: " v)]))

[module+ test
(require typed/rackunit
(require rackunit
racket/port)

;; TODO: Replace with this, but fails to typecheck.
Expand Down
Loading