Skip to content
This repository has been archived by the owner on Jul 28, 2019. It is now read-only.

Commit

Permalink
Add string-conv module.
Browse files Browse the repository at this point in the history
Also fix module loading from #lang.
  • Loading branch information
juanibiapina committed Nov 23, 2014
1 parent 107e650 commit 1f42cb4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
7 changes: 4 additions & 3 deletions examples/projecteuler/problem-1.mrc
@@ -1,7 +1,8 @@
(def :io (require "io"))
(def :integer (require "integer"))
(def :string-conv (require "string-conv"))
(def :integers (require "integers"))

(def :n (integer.parse (io.read-line io.stdin)))
(def :n (string-conv.parse-int (io.read-line io.stdin) 10))

(def :sum (function [:list] {
(if (nil? list) { 0 }
Expand All @@ -12,6 +13,6 @@
(or { (= (% n 3) 0) } { (= (% n 5) 0) })
}))

(def :result (sum (filter (integer.range 1 n) include?)))
(def :result (sum (filter (integers.range 1 n) include?)))

(print result)
3 changes: 1 addition & 2 deletions lang/marco.rkt
@@ -1,12 +1,11 @@
#lang racket

(require
"../interpreter.rkt"
"../loader.rkt"
"../core-env.rkt")

(provide
#%datum
make-core-env
eval
(rename-out [module-begin #%module-begin]))

Expand Down
17 changes: 13 additions & 4 deletions lang/reader.rkt
Expand Up @@ -9,14 +9,23 @@ marco/lang/marco
"lexer.rkt"
"parser.rkt"
"../interpreter.rkt"
"../core-env.rkt")
"../core-env.rkt"
(prefix-in m: "../language/main.rkt"))

(define (marco-read in)
(map syntax->datum (marco-read-syntax #f in)))

(define (marco-read-syntax src in)
(let* ([token-gen (make-token-gen in src)]
[ast (parse token-gen)])
(with-syntax ([ast ast])
[ast (parse token-gen)]
[env (make-core-env)]
[env (m:extend
env
"module-path"
(m:string
(path->string
(let-values ([(path name _) (split-path (collection-file-path "main.rkt" "marco"))])
(build-path path "modules")))))])
(with-syntax ([ast ast] [env env])
(list
(syntax (eval ast (make-core-env)))))))
(syntax (eval ast env))))))
1 change: 1 addition & 0 deletions modules/integers.rkt
@@ -0,0 +1 @@
#lang racket
25 changes: 25 additions & 0 deletions modules/string-conv.rkt
@@ -0,0 +1,25 @@
#lang racket

(require
(prefix-in m: "../language/main.rkt"))

(provide
module)

(define parse-int-fun
(m:function
(list "value" "radix")
(m:native-block
(lambda (closure dynamic)
(let* ([value (m:lookup closure "value")]
[radix (m:lookup closure "radix")])
(m:integer (string->number (m:string-v value) (m:integer-v radix))))))))

(define module
(let ([env (m:make-env)])
(m:module
env
(list
(cons
"parse-int"
(m:closure env parse-int-fun))))))
7 changes: 7 additions & 0 deletions test/modules/string-conv/parse-int.rkt
@@ -0,0 +1,7 @@
#lang marco

(def :string-conv (require "string-conv"))

(def :one (string-conv.parse-int "1" 10))

(check-equal? one 1)

0 comments on commit 1f42cb4

Please sign in to comment.