Compare commits

..

No commits in common. 'master' and 'dev-srcloc' have entirely different histories.

@ -1,72 +0,0 @@
name: CI
on: [push, pull_request]
jobs:
run:
name: "Build using Racket '${{ matrix.racket-version }}' (${{ matrix.racket-variant }})"
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.xfail == 'yes' }}
strategy:
fail-fast: false
matrix:
racket-version: ["6.7", "6.8", "6.9", "6.11", "6.12", "7.0", "7.1", "7.2", "7.3", "7.4", "7.5", "7.6", "7.7", "7.8", "7.9", "8.0", "8.1", "current"]
racket-variant: ["BC", "CS"]
# CS builds are only provided for versions 7.4 and up so avoid
# running the job for prior versions.
exclude:
- {racket-version: "6.6", racket-variant: "CS"}
- {racket-version: "6.7", racket-variant: "CS"}
- {racket-version: "6.8", racket-variant: "CS"}
- {racket-version: "6.9", racket-variant: "CS"}
- {racket-version: "6.10.1", racket-variant: "CS"}
- {racket-version: "6.11", racket-variant: "CS"}
- {racket-version: "6.12", racket-variant: "CS"}
- {racket-version: "7.0", racket-variant: "CS"}
- {racket-version: "7.1", racket-variant: "CS"}
- {racket-version: "7.2", racket-variant: "CS"}
- {racket-version: "7.3", racket-variant: "CS"}
include:
- {racket-version: "6.7", racket-variant: "BC", xfail: "yes"}
- {racket-version: "7.7", racket-variant: "CS", xfail: "yes"}
- {racket-version: "7.8", racket-variant: "CS", xfail: "yes"}
- {racket-version: "7.9", racket-variant: "CS", xfail: "yes"}
- {racket-version: "current", racket-variant: "BC", xfail: "yes"}
- {racket-version: "current", racket-variant: "CS", xfail: "yes"}
env:
DISPLAY: :99
steps:
- name: Checkout
uses: actions/checkout@master
- uses: Bogdanp/setup-racket@v0.11
with:
distribution: 'full'
version: ${{ matrix.racket-version }}
variant: ${{ matrix.racket-variant }}
- name: Install package and its dependencies
run: raco pkg install --deps search-auto https://github.com/mbutterick/br-parser-tools.git?path=br-parser-tools-lib
- name: install brag
run: raco pkg install --deps search-auto https://github.com/mbutterick/brag.git?path=brag
- name: install br-macro
run: raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=beautiful-racket-macro
- name: install br lib
run: raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=beautiful-racket-lib
- name: install br demo
run: raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=beautiful-racket-demo
- name: install br
run: raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=beautiful-racket
- name: Start virtual framebuffer
run: Xvfb "$DISPLAY" -screen 0 1280x1024x24 &
- name: Run the brm tests
run: raco test -p beautiful-racket-macro
- name: Run the lib tests
run: raco test -p beautiful-racket-lib
- name: Run the demo tests
run: raco test -p beautiful-racket-demo
- name: Run the br tests
run: raco test -p beautiful-racket

3
.gitignore vendored

@ -21,5 +21,4 @@ scribblings/*.css
!scribblings/mb.css !scribblings/mb.css
scribblings/*.html scribblings/*.html
scribblings/pollen/* scribblings/pollen/*
beautiful-racket-lib/br/doc/* doc/*
beautiful-racket-demo/jsonic-demo-3/doc/*

@ -0,0 +1,48 @@
# adapted from
# https://github.com/greghendershott/travis-racket/blob/master/.travis.yml
# Thanks Greg!
language: c
sudo: false
env:
global:
- RACKET_DIR=~/racket
matrix:
# - RACKET_VERSION=6.0
# - RACKET_VERSION=6.1
# - RACKET_VERSION=6.2
- RACKET_VERSION=6.3
- RACKET_VERSION=6.4
- RACKET_VERSION=6.5
- RACKET_VERSION=6.6
- RACKET_VERSION=HEAD
# You may want to test against certain versions of Racket, without
# having them count against the overall success/failure.
matrix:
allow_failures:
#- env: RACKET_VERSION=HEAD
# Fast finish: Overall build result is determined as soon as any of
# its rows have failed, or, all of its rows that aren't allowed to
# fail have succeeded.
fast_finish: true
before_install:
- "export DISPLAY=:99.0" # needed for testing with `racket/gui`
- "sh -e /etc/init.d/xvfb start" # needed for testing with `racket/gui`
- git clone https://github.com/mbutterick/travis-racket.git
- cat travis-racket/install-racket.sh | bash # pipe to bash not sh!
- export PATH="${RACKET_DIR}/bin:${PATH}" #install-racket.sh can't set for us
script:
- cd .. # Travis did a cd into the dir. Back up, for the next:
# don't rely on package server
- travis_retry raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=beautiful-racket-lib
- raco test -p beautiful-racket-lib
- travis_retry raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=brag
- raco test -p brag
- travis_retry raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=beautiful-racket-demo
- raco test -p beautiful-racket-demo
- travis_retry raco pkg install --deps search-auto https://github.com/mbutterick/beautiful-racket.git?path=beautiful-racket
- raco test -p beautiful-racket

@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

@ -0,0 +1,3 @@
Beautiful Racket
© 2016 Matthew Butterick
Licensed under the LGPL (see "LGPL.txt")

@ -1,9 +0,0 @@
MIT License for `beautiful-racket`
© 2016-2019 Matthew Butterick
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ -1,34 +1,23 @@
## beautiful-racket [![Build Status](https://github.com/mbutterick/beautiful-racket/workflows/CI/badge.svg)](https://github.com/mbutterick/beautiful-racket/actions) beautiful-racket [![Build Status](https://travis-ci.org/mbutterick/beautiful-racket.svg?branch=master)](https://travis-ci.org/mbutterick/beautiful-racket)
-
Resources for the upcoming “Beautiful Racket” book, including:
Resources for the “Beautiful Racket” book, including:
* `#lang br` teaching language * `#lang br` teaching language
* `#lang brag` parser generator language (a fork of Danny Yoo's [ragg](http://github.com/jbclements/ragg))
* supporting modules * supporting modules
* sample languages * sample languages
## Installation
`raco pkg install beautiful-racket`
## Update
`raco pkg update --update-deps beautiful-racket` Installation:
## Documentation `raco pkg install beautiful-racket`
http://docs.racket-lang.org/br
## License
MIT
## Project status Update:
Complete. I will maintain the code but no major updates are planned. `raco pkg update beautiful-racket`

@ -1,6 +0,0 @@
#lang brag
top : (fun | app)*
fun : /"fun" ID /"(" ID [/"," ID] /")" /"=" expr
expr : ID /"+" ID | app
app : ID /"(" (ID | INT) [/"," ID] /")"

@ -1,36 +0,0 @@
#lang br/quicklang
(require brag/support "grammar.rkt")
(provide top fun expr app)
(module+ reader
(provide read-syntax))
(define-lex-abbrev reserved-toks
(:or "fun" "(" ")" "=" "+" ","))
(define tokenize-1
(lexer
[whitespace (token lexeme #:skip? #t)]
[(from/stop-before "#" "\n") (token 'COMMENT #:skip? #t)]
[reserved-toks lexeme]
[(:+ alphabetic) (token 'ID (string->symbol lexeme))]
[(:+ (char-set "0123456789")) (token 'INT (string->number lexeme))]))
(define-macro top #'#%module-begin)
(define-macro-cases fun
[(_ VAR ARG0 EXPR) #'(define (VAR ARG0) EXPR)]
[(_ VAR ARG0 ARG1 EXPR) #'(define (VAR ARG0 ARG1) EXPR)])
(define-macro-cases expr
[(_ LEFT RIGHT) #'(+ LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro app #'#%app)
(define (read-syntax src ip)
(define parse-tree (parse src (λ () (tokenize-1 ip))))
(strip-bindings
(with-syntax ([PT parse-tree])
#'(module algebra-mod algebra-demo
PT))))

@ -1,6 +0,0 @@
#lang algebra-demo
fun f(x,y) = x + y
# fun f(x,y) = x * y
fun g(zz) = f(zz,zz)
g(10)
g(23)

@ -1,16 +0,0 @@
#lang br
(provide #%top-interaction #%module-begin
(rename-out [my-datum #%datum]
[my-datum #%top]
[my-app #%app]))
(define-syntax (my-datum stx)
(syntax-case stx ()
[(_ . THING) #''taco]))
(define-syntax (my-app stx)
(syntax-case stx ()
[(_ FUNC . ARGS) #'(list (my-datum) . ARGS)]))
(module reader syntax/module-reader
atomic-taco-demo)

@ -1,4 +0,0 @@
#lang atomic-taco-demo
"hello world"
(+ 1 (* 2 (- x)))

@ -1,32 +0,0 @@
#lang br
(require "lexer.rkt" brag/support)
(provide basic-colorer)
(define (basic-colorer port)
(define (handle-lexer-error excn)
(define excn-srclocs (exn:fail:read-srclocs excn))
(srcloc-token (token 'ERROR) (car excn-srclocs)))
(define srcloc-tok
(with-handlers ([exn:fail:read? handle-lexer-error])
(basic-lexer port)))
(match srcloc-tok
[(? eof-object?) (values srcloc-tok 'eof #f #f #f)]
[else
(match-define
(srcloc-token
(token-struct type val _ _ _ _ _)
(srcloc _ _ _ posn span)) srcloc-tok)
(define start posn)
(define end (+ start span))
(match-define (list cat paren)
(match type
['STRING '(string #f)]
['REM '(comment #f)]
['ERROR '(error #f)]
[else (match val
[(? number?) '(constant #f)]
[(? symbol?) '(symbol #f)]
["(" '(parenthesis |(|)]
[")" '(parenthesis |)|)]
[else '(no-color #f)])]))
(values val cat paren start end)]))

@ -1,41 +0,0 @@
#lang br
(require "go.rkt")
(provide b-if b-or-expr b-and-expr b-not-expr b-comp-expr)
(define (bool->int val) (if val 1 0))
(define nonzero? (compose1 not zero?))
(define-macro-cases b-or-expr
[(_ VAL) #'VAL]
[(_ LEFT "or" RIGHT)
#'(bool->int (or (nonzero? LEFT) (nonzero? RIGHT)))])
(define-macro-cases b-and-expr
[(_ VAL) #'VAL]
[(_ LEFT "and" RIGHT)
#'(bool->int (and (nonzero? LEFT) (nonzero? RIGHT)))])
(define-macro-cases b-not-expr
[(_ VAL) #'VAL]
[(_ "not" VAL) #'(if (nonzero? VAL) 0 1)])
(define b= (compose1 bool->int =))
(define b< (compose1 bool->int <))
(define b> (compose1 bool->int >))
(define b<> (compose1 bool->int not =))
(define-macro-cases b-comp-expr
[(_ VAL) #'VAL]
[(_ LEFT "=" RIGHT) #'(b= LEFT RIGHT)]
[(_ LEFT "<" RIGHT) #'(b< LEFT RIGHT)]
[(_ LEFT ">" RIGHT) #'(b> LEFT RIGHT)]
[(_ LEFT "<>" RIGHT) #'(b<> LEFT RIGHT)])
(define-macro-cases b-if
[(_ COND-EXPR THEN-EXPR) #'(b-if COND-EXPR THEN-EXPR (void))]
[(_ COND-EXPR THEN-EXPR ELSE-EXPR)
#'(let ([result (if (nonzero? COND-EXPR)
THEN-EXPR
ELSE-EXPR)])
(when (exact-positive-integer? result)
(b-goto result)))])

@ -1,6 +0,0 @@
#lang br
(require "line.rkt" "go.rkt"
"expr.rkt" "misc.rkt" "cond.rkt")
(provide
(all-from-out "line.rkt" "go.rkt"
"expr.rkt" "misc.rkt" "cond.rkt"))

@ -1,26 +0,0 @@
#lang br/quicklang
(require "struct.rkt" "run.rkt" "elements.rkt")
(provide (rename-out [b-module-begin #%module-begin])
(all-from-out "elements.rkt"))
(define-macro (b-module-begin (b-program LINE ...))
(with-pattern
([((b-line NUM STMT ...) ...) #'(LINE ...)]
[(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))]
[(VAR-ID ...) (find-unique-var-ids #'(LINE ...))])
#'(#%module-begin
(define VAR-ID 0) ...
LINE ...
(define line-table
(apply hasheqv (append (list NUM LINE-FUNC) ...)))
(void (run line-table)))))
(begin-for-syntax
(require racket/list)
(define (find-unique-var-ids line-stxs)
(remove-duplicates
(for/list ([stx (in-list (stx-flatten line-stxs))]
#:when (syntax-property stx 'b-id))
stx)
#:key syntax->datum)))

@ -1,24 +0,0 @@
#lang br
(provide b-expr b-sum b-product b-neg b-expt)
(define (b-expr expr)
(if (integer? expr) (inexact->exact expr) expr))
(define-macro-cases b-sum
[(_ VAL) #'VAL]
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)])
(define-macro-cases b-product
[(_ VAL) #'VAL]
[(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)]
[(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT 1.0)]
[(_ LEFT "mod" RIGHT) #'(modulo LEFT RIGHT)])
(define-macro-cases b-neg
[(_ VAL) #'VAL]
[(_ "-" VAL) #'(- VAL)])
(define-macro-cases b-expt
[(_ VAL) #'VAL]
[(_ LEFT "^" RIGHT) #'(expt LEFT RIGHT)])

@ -1,52 +0,0 @@
#lang br
(require "struct.rkt" "line.rkt" "misc.rkt")
(provide b-end b-goto b-gosub b-return b-for b-next)
(define (b-end) (raise (end-program-signal)))
(define (b-goto num-expr)
(raise (change-line-signal num-expr)))
(define return-ccs empty)
(define (b-gosub num-expr)
(let/cc this-cc
(push! return-ccs this-cc)
(b-goto num-expr)))
(define (b-return)
(when (empty? return-ccs)
(raise-line-error "return without gosub"))
(define top-cc (pop! return-ccs))
(top-cc (void)))
(define next-funcs (make-hasheq))
(define-macro-cases b-for
[(_ LOOP-ID START END) #'(b-for LOOP-ID START END 1)]
[(_ LOOP-ID START END STEP)
#'(b-let LOOP-ID
(let/cc loop-cc
(hash-set! next-funcs
'LOOP-ID
(λ ()
(define next-val
(+ LOOP-ID STEP))
(if (next-val
. in-closed-interval? .
START END)
(loop-cc next-val)
(hash-remove! next-funcs
'LOOP-ID))))
START))])
(define (in-closed-interval? x start end)
((if (< start end) <= >=) start x end))
(define-macro (b-next LOOP-ID)
#'(begin
(unless (hash-has-key? next-funcs 'LOOP-ID)
(raise-line-error
(format "`next ~a` without for" 'LOOP-ID)))
(define func (hash-ref next-funcs 'LOOP-ID))
(func)))

@ -1,4 +0,0 @@
#lang info
(define compile-omit-paths '("private"))
(define test-omit-paths '("private" "sample-input.rkt"))

@ -1,25 +0,0 @@
#lang br
(require brag/support)
(define-lex-abbrev digits (:+ (char-set "0123456789")))
(define-lex-abbrev reserved-terms (:or "print" "goto" "end" "+" ":" ";" "let" "=" "input" "-" "*" "/" "^" "mod" "(" ")" "if" "then" "else" "<" ">" "<>" "and" "or" "not" "gosub" "return" "for" "to" "step" "next"))
(define basic-lexer
(lexer-srcloc
["\n" (token 'NEWLINE lexeme)]
[whitespace (token lexeme #:skip? #t)]
[(from/stop-before "rem" "\n") (token 'REM lexeme)]
[reserved-terms (token lexeme lexeme)]
[(:seq alphabetic (:* (:or alphabetic numeric "$")))
(token 'ID (string->symbol lexeme))]
[digits (token 'INTEGER (string->number lexeme))]
[(:or (:seq (:? digits) "." digits)
(:seq digits "."))
(token 'DECIMAL (string->number lexeme))]
[(:or (from/to "\"" "\"") (from/to "'" "'"))
(token 'STRING
(substring lexeme
1 (sub1 (string-length lexeme))))]))
(provide basic-lexer)

@ -1,22 +0,0 @@
#lang br
(require "struct.rkt")
(provide b-line raise-line-error)
(define-macro (b-line NUM STATEMENT ...)
(with-pattern ([LINE-NUM (prefix-id "line-" #'NUM
#:source #'NUM)])
(syntax/loc caller-stx
(define (LINE-NUM #:error [msg #f])
(with-handlers
([line-error?
(λ (le) (handle-line-error NUM le))])
(when msg (raise-line-error msg))
STATEMENT ...)))))
(define (raise-line-error error-msg)
(raise (line-error error-msg)))
(define (handle-line-error num le)
(error (format "error in line ~a: ~a"
num (line-error-msg le))))

@ -1,19 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(module+ reader
(provide read-syntax get-info))
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-mod basic-demo-2/expander
#,parse-tree)))
(define (get-info port src-mod src-line src-col src-pos)
(define (handle-query key default)
(case key
[(color-lexer)
(dynamic-require 'basic-demo-2/colorer 'basic-colorer)]
[else default]))
handle-query)

@ -1,15 +0,0 @@
#lang br
(require "struct.rkt")
(provide b-rem b-print b-let b-input)
(define (b-rem val) (void))
(define (b-print . vals)
(displayln (string-append* (map ~a vals))))
(define-macro (b-let ID VAL) #'(set! ID VAL))
(define-macro (b-input ID)
#'(b-let ID (let* ([str (read-line)]
[num (string->number (string-trim str))])
(or num str))))

@ -1,14 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-parser-mod basic-demo-2/parse-only
#,parse-tree)))
(module+ reader (provide read-syntax))
(define-macro (parser-only-mb PARSE-TREE)
#'(#%module-begin
'PARSE-TREE))
(provide (rename-out [parser-only-mb #%module-begin]))

@ -1,15 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-parser-mod basic-demo-2/parse-stx
#'#,parse-tree)))
(module+ reader (provide read-syntax))
(define-macro (parser-only-mb PARSE-STX)
#'(#%module-begin
PARSE-STX))
(provide (rename-out [parser-only-mb #%module-begin]))
(provide syntax)

@ -1,32 +0,0 @@
#lang brag
b-program : [b-line] (/NEWLINE [b-line])*
b-line : b-line-num [b-statement] (/":" [b-statement])* [b-rem]
@b-line-num : INTEGER
b-rem : REM
@b-statement : b-end | b-print | b-goto
| b-let | b-input | b-if
| b-gosub | b-return | b-for | b-next
b-end : /"end"
b-print : /"print" [b-printable] (/";" [b-printable])*
@b-printable : STRING | b-expr
b-goto : /"goto" b-expr
b-let : [/"let"] b-id /"=" (STRING | b-expr)
b-if : /"if" b-expr /"then" (b-statement | b-expr)
[/"else" (b-statement | b-expr)]
b-input : /"input" b-id
@b-id : ID
b-gosub : /"gosub" b-expr
b-return : /"return"
b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr]
b-next : /"next" b-id
b-expr : b-or-expr
b-or-expr : [b-or-expr "or"] b-and-expr
b-and-expr : [b-and-expr "and"] b-not-expr
b-not-expr : ["not"] b-comp-expr
b-comp-expr : [b-comp-expr ("="|"<"|">"|"<>")] b-sum
b-sum : [b-sum ("+"|"-")] b-product
b-product : [b-product ("*"|"/"|"mod")] b-neg
b-neg : ["-"] b-expt
b-expt : [b-expt ("^")] b-value
@b-value : b-number | b-id | /"(" b-expr /")"
@b-number : INTEGER | DECIMAL

@ -1,8 +0,0 @@
(define (30) (rem print "'ignored'"))
(define (35) (void))
(define (50) (print "never gets here"))
(define (40) (end))
(define (60) (print "three") (print (+ 1.0 3)))
(define (70) (goto (+ 11 18.5 0.5)))
(define (10) (print "one"))
(define (20) (print) (goto 60) (end))

@ -1,23 +0,0 @@
#lang br
(require "line.rkt" "struct.rkt")
(provide run)
(define (run line-table)
(define line-vec
(list->vector (sort (hash-keys line-table) <)))
(with-handlers ([end-program-signal? (λ (exn-val) (void))])
(for/fold ([line-idx 0])
([i (in-naturals)]
#:break (>= line-idx (vector-length line-vec)))
(define line-num (vector-ref line-vec line-idx))
(define line-func (hash-ref line-table line-num))
(with-handlers
([change-line-signal?
(λ (cls)
(define clsv (change-line-signal-val cls))
(or
(and (exact-positive-integer? clsv)
(vector-member clsv line-vec))
(line-func #:error (format "line ~a not found" clsv))))])
(line-func)
(add1 line-idx)))))

@ -1,8 +0,0 @@
#lang basic-demo-2
10 rem all results should be 1
20 a = 5
30 b = 10
40 print a > 4
50 print b = 10
60 print b < 11
70 print rem (b = 100)

@ -1,6 +0,0 @@
#lang basic-demo-2
10 for a = 1 to 3
21 for b = 9 to 7 step -1
22 print a ; b
23 next b
30 next a

@ -1,10 +0,0 @@
#lang basic-demo-2
10 gosub 41
20 print "world"
30 gosub 100
31 print "hi"
35 end
40 return
41 print "hello" : return
100 print "third"
110 goto 40

@ -1,4 +0,0 @@
#lang basic-demo-2
5 print "enter your name: "
10 input A$
20 print "hello, " ; A$ ; "!"

@ -1,9 +0,0 @@
#lang basic-demo-2
10 rem all results should be 1
20 print 1 - 2 * 3 + 4 * 5 - 6 = 9
30 print (1 - 2) * (3 + 4) * (5 - 6) = 7
40 print 1 / 4 = .25
50 print 2 ^ 3 = 8
60 print 9 ^ 0.5 = 3
70 print 6 mod 2 = 0
80 print 5 mod 2 = 1

@ -1,8 +0,0 @@
#lang basic-demo-2
10 a = 1 : a = 5
20 gosub 150
30 a = 25
40 gosub 150
50 end
150 print a + a + a
160 return

@ -1,9 +0,0 @@
#lang basic-demo-2
30 rem print 'ignored'
35
50 print "never gets here"
40 end
60 print 'three' : print 1.0 + 3
70 goto 11. + 18.5 + .5 rem ignored
10 print "o" ; "n" ; "e"
20 print : goto 60.0 : end

@ -1,8 +0,0 @@
#lang br
(provide (struct-out end-program-signal)
(struct-out change-line-signal)
(struct-out line-error))
(struct end-program-signal ())
(struct change-line-signal (val))
(struct line-error (msg))

@ -1,34 +0,0 @@
#lang at-exp br
(require br/test rackunit)
(check-equal? (run-source "sample.rkt") "one
three
4
")
(check-equal? (run-source "sample-var.rkt") "15
75
")
(check-equal? (run-source "sample-math.rkt") "1
1
1
1
1
1
1
")
(check-equal? (run-source "sample-gosub.rkt") "hello
world
third
hi
")
(check-equal? (run-source "sample-for.rkt") "19
18
17
29
28
27
39
38
37
")

@ -1,10 +0,0 @@
#lang br
(require "lexer.rkt" brag/support)
(define (make-tokenizer ip [path #f])
(port-count-lines! ip)
(lexer-file-path path)
(define (next-token) (basic-lexer ip))
next-token)
(provide make-tokenizer)

@ -1,6 +0,0 @@
#lang br
(require "line.rkt" "go.rkt"
"expr.rkt" "misc.rkt")
(provide
(all-from-out "line.rkt" "go.rkt"
"expr.rkt" "misc.rkt"))

@ -1,16 +0,0 @@
#lang br/quicklang
(require "struct.rkt" "run.rkt" "elements.rkt")
(provide (rename-out [b-module-begin #%module-begin])
(all-from-out "elements.rkt"))
(define-macro (b-module-begin (b-program LINE ...))
(with-pattern
([((b-line NUM STMT ...) ...) #'(LINE ...)]
[(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))])
#'(#%module-begin
LINE ...
(define line-table
(apply hasheqv (append (list NUM LINE-FUNC) ...)))
(void (run line-table)))))

@ -1,7 +0,0 @@
#lang br
(provide b-sum b-expr)
(define (b-sum . vals) (apply + vals))
(define (b-expr expr)
(if (integer? expr) (inexact->exact expr) expr))

@ -1,8 +0,0 @@
#lang br
(require "struct.rkt" "line.rkt")
(provide b-end b-goto)
(define (b-end) (raise (end-program-signal)))
(define (b-goto num-expr)
(raise (change-line-signal num-expr)))

@ -1,22 +0,0 @@
#lang br
(require brag/support)
(define-lex-abbrev digits (:+ (char-set "0123456789")))
(define basic-lexer
(lexer-srcloc
["\n" (token 'NEWLINE lexeme)]
[whitespace (token lexeme #:skip? #t)]
[(from/stop-before "rem" "\n") (token 'REM lexeme)]
[(:or "print" "goto" "end"
"+" ":" ";") (token lexeme lexeme)]
[digits (token 'INTEGER (string->number lexeme))]
[(:or (:seq (:? digits) "." digits)
(:seq digits "."))
(token 'DECIMAL (string->number lexeme))]
[(:or (from/to "\"" "\"") (from/to "'" "'"))
(token 'STRING
(substring lexeme
1 (sub1 (string-length lexeme))))]))
(provide basic-lexer)

@ -1,9 +0,0 @@
#lang br
(require "struct.rkt")
(provide b-line)
(define-macro (b-line NUM STATEMENT ...)
(with-pattern ([LINE-NUM (prefix-id "line-" #'NUM
#:source #'NUM)])
(syntax/loc caller-stx
(define (LINE-NUM) (void) STATEMENT ...))))

@ -1,11 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(module+ reader
(provide read-syntax))
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-mod basic-demo-2a/expander
#,parse-tree)))

@ -1,8 +0,0 @@
#lang br
(require "struct.rkt")
(provide b-rem b-print)
(define (b-rem val) (void))
(define (b-print . vals)
(displayln (string-append* (map ~a vals))))

@ -1,13 +0,0 @@
#lang brag
b-program : [b-line] (/NEWLINE [b-line])*
b-line : b-line-num [b-statement] (/":" [b-statement])* [b-rem]
@b-line-num : INTEGER
@b-statement : b-end | b-print | b-goto
b-rem : REM
b-end : /"end"
b-print : /"print" [b-printable] (/";" [b-printable])*
@b-printable : STRING | b-expr
b-goto : /"goto" b-expr
b-expr : b-sum
b-sum : b-number (/"+" b-number)*
@b-number : INTEGER | DECIMAL

@ -1,24 +0,0 @@
#lang br
(require "line.rkt" "struct.rkt")
(provide run)
(define (run line-table)
(define line-vec
(list->vector (sort (hash-keys line-table) <)))
(with-handlers ([end-program-signal? (λ (exn-val) (void))])
(for/fold ([line-idx 0])
([i (in-naturals)]
#:break (>= line-idx (vector-length line-vec)))
(define line-num (vector-ref line-vec line-idx))
(define line-func (hash-ref line-table line-num))
(with-handlers
([change-line-signal?
(λ (cls)
(define clsv (change-line-signal-val cls))
(or
(and (exact-positive-integer? clsv)
(vector-member clsv line-vec))
(error (format "error in line ~a: line ~a not found"
line-num clsv))))])
(line-func)
(add1 line-idx)))))

@ -1,9 +0,0 @@
#lang basic-demo
30 rem print 'ignored'
35
50 print "never gets here"
40 end
60 print 'three' : print 1.0 + 3
70 goto 11. + 18.5 + .5 rem ignored
10 print "o" ; "n" ; "e"
20 print : goto 60.0 : end

@ -1,6 +0,0 @@
#lang br
(provide (struct-out end-program-signal)
(struct-out change-line-signal))
(struct end-program-signal ())
(struct change-line-signal (val))

@ -1,8 +0,0 @@
#lang at-exp br
(require br/test rackunit)
(check-equal? (run-source "sample.rkt") "one
three
4
")

@ -1,10 +0,0 @@
#lang br
(require "lexer.rkt" brag/support)
(define (make-tokenizer ip [path #f])
(port-count-lines! ip)
(lexer-file-path path)
(define (next-token) (basic-lexer ip))
next-token)
(provide make-tokenizer)

@ -1,2 +0,0 @@
#lang br
(current-command-line-arguments)

@ -1,32 +0,0 @@
#lang br
(require "lexer.rkt" brag/support)
(provide basic-colorer)
(define (basic-colorer port)
(define (handle-lexer-error excn)
(define excn-srclocs (exn:fail:read-srclocs excn))
(srcloc-token (token 'ERROR) (car excn-srclocs)))
(define srcloc-tok
(with-handlers ([exn:fail:read? handle-lexer-error])
(basic-lexer port)))
(match srcloc-tok
[(? eof-object?) (values srcloc-tok 'eof #f #f #f)]
[else
(match-define
(srcloc-token
(token-struct type val _ _ _ _ _)
(srcloc _ _ _ posn span)) srcloc-tok)
(define start posn)
(define end (+ start span))
(match-define (list cat paren)
(match type
['STRING '(string #f)]
['REM '(comment #f)]
['ERROR '(error #f)]
[else (match val
[(? number?)'(constant #f)]
[(? symbol?) '(symbol #f)]
["(" '(parenthesis |(|)]
[")" '(parenthesis |)|)]
[else '(no-color #f)])]))
(values val cat paren start end)]))

@ -1,41 +0,0 @@
#lang br
(require "go.rkt")
(provide b-if b-or-expr b-and-expr b-not-expr b-comp-expr)
(define (bool->int val) (if val 1 0))
(define nonzero? (compose1 not zero?))
(define-macro-cases b-or-expr
[(_ VAL) #'VAL]
[(_ LEFT "or" RIGHT)
#'(bool->int (or (nonzero? LEFT) (nonzero? RIGHT)))])
(define-macro-cases b-and-expr
[(_ VAL) #'VAL]
[(_ LEFT "and" RIGHT)
#'(bool->int (and (nonzero? LEFT) (nonzero? RIGHT)))])
(define-macro-cases b-not-expr
[(_ VAL) #'VAL]
[(_ "not" VAL) #'(if (nonzero? VAL) 0 1)])
(define b= (compose1 bool->int =))
(define b< (compose1 bool->int <))
(define b> (compose1 bool->int >))
(define b<> (compose1 bool->int not =))
(define-macro-cases b-comp-expr
[(_ VAL) #'VAL]
[(_ LEFT "=" RIGHT) #'(b= LEFT RIGHT)]
[(_ LEFT "<" RIGHT) #'(b< LEFT RIGHT)]
[(_ LEFT ">" RIGHT) #'(b> LEFT RIGHT)]
[(_ LEFT "<>" RIGHT) #'(b<> LEFT RIGHT)])
(define-macro-cases b-if
[(_ COND-EXPR THEN-EXPR) #'(b-if COND-EXPR THEN-EXPR (void))]
[(_ COND-EXPR THEN-EXPR ELSE-EXPR)
#'(let ([result (if (nonzero? COND-EXPR)
THEN-EXPR
ELSE-EXPR)])
(when (exact-positive-integer? result)
(b-goto result)))])

@ -1,6 +0,0 @@
#lang br
(require "line.rkt" "go.rkt"
"expr.rkt" "misc.rkt" "cond.rkt")
(provide
(all-from-out "line.rkt" "go.rkt"
"expr.rkt" "misc.rkt" "cond.rkt"))

@ -1,58 +0,0 @@
#lang br/quicklang
(require "struct.rkt" "run.rkt" "elements.rkt" "setup.rkt")
(provide (rename-out [b-module-begin #%module-begin])
(all-from-out "elements.rkt"))
(define-macro (b-module-begin (b-program LINE ...))
(with-pattern
([((b-line NUM STMT ...) ...) #'(LINE ...)]
[(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))]
[(VAR-ID ...) (find-property 'b-id #'(LINE ...))]
[(IMPORT-NAME ...)
(find-property 'b-import-name #'(LINE ...))]
[(EXPORT-NAME ...)
(find-property 'b-export-name #'(LINE ...))]
[((SHELL-ID SHELL-IDX) ...)
(make-shell-ids-and-idxs caller-stx)]
[(UNIQUE-ID ...)
(unique-ids
(syntax->list #'(VAR-ID ... SHELL-ID ...)))])
#'(#%module-begin
(module configure-runtime br
(require basic-demo-3/setup)
(do-setup!))
(require IMPORT-NAME) ...
(provide EXPORT-NAME ...)
(define UNIQUE-ID 0) ...
(let ([clargs (current-command-line-arguments)])
(set! SHELL-ID (get-clarg clargs SHELL-IDX)) ...)
LINE ...
(define line-table
(apply hasheqv (append (list NUM LINE-FUNC) ...)))
(parameterize
([current-output-port (basic-output-port)])
(void (run line-table))))))
(define (get-clarg clargs idx)
(if (<= (vector-length clargs) idx)
0
(let ([val (vector-ref clargs idx)])
(or (string->number val) val))))
(begin-for-syntax
(require racket/list)
(define (unique-ids stxs)
(remove-duplicates stxs #:key syntax->datum))
(define (find-property which line-stxs)
(unique-ids
(for/list ([stx (in-list (stx-flatten line-stxs))]
#:when (syntax-property stx which))
stx)))
(define (make-shell-ids-and-idxs ctxt)
(define arg-count 10)
(for/list ([idx (in-range arg-count)])
(list (suffix-id #'arg idx #:context ctxt) idx))))

@ -1,45 +0,0 @@
#lang br
(require "line.rkt")
(provide b-expr b-sum b-product b-neg b-expt b-def b-func)
(define (b-expr expr)
(if (integer? expr) (inexact->exact expr) expr))
(define-macro-cases b-sum
[(_ VAL) #'VAL]
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)])
(define-macro-cases b-product
[(_ VAL) #'VAL]
[(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)]
[(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT 1.0)]
[(_ LEFT "mod" RIGHT) #'(modulo LEFT RIGHT)])
(define-macro-cases b-neg
[(_ VAL) #'VAL]
[(_ "-" VAL) #'(- VAL)])
(define-macro-cases b-expt
[(_ VAL) #'VAL]
[(_ LEFT "^" RIGHT) #'(expt LEFT RIGHT)])
(define-macro (b-def FUNC-ID VAR-ID ... EXPR)
(syntax-local-lift-expression
#'(set! FUNC-ID (λ (VAR-ID ...) EXPR))))
(define-macro (b-func FUNC-ID ARG ...)
#'(if (procedure? FUNC-ID)
(convert-result (FUNC-ID ARG ...))
(raise-line-error
(format "expected ~a to be a function, got ~v"
'FUNC-ID FUNC-ID))))
(define (convert-result result)
(cond
[(number? result) (b-expr result)]
[(string? result) result]
[(boolean? result) (if result 1 0)]
[else
(raise-line-error
(format "unknown data type: ~v" result))]))

@ -1,52 +0,0 @@
#lang br
(require "struct.rkt" "line.rkt" "misc.rkt")
(provide b-end b-goto b-gosub b-return b-for b-next)
(define (b-end) (raise (end-program-signal)))
(define (b-goto num-expr)
(raise (change-line-signal num-expr)))
(define return-ccs empty)
(define (b-gosub num-expr)
(let/cc this-cc
(push! return-ccs this-cc)
(b-goto num-expr)))
(define (b-return)
(when (empty? return-ccs)
(raise-line-error "return without gosub"))
(define top-cc (pop! return-ccs))
(top-cc (void)))
(define next-funcs (make-hasheq))
(define-macro-cases b-for
[(_ LOOP-ID START END) #'(b-for LOOP-ID START END 1)]
[(_ LOOP-ID START END STEP)
#'(b-let LOOP-ID
(let/cc loop-cc
(hash-set! next-funcs
'LOOP-ID
(λ ()
(define next-val
(+ LOOP-ID STEP))
(if (next-val
. in-closed-interval? .
START END)
(loop-cc next-val)
(hash-remove! next-funcs
'LOOP-ID))))
START))])
(define (in-closed-interval? x start end)
((if (< start end) <= >=) start x end))
(define-macro (b-next LOOP-ID)
#'(begin
(unless (hash-has-key? next-funcs 'LOOP-ID)
(raise-line-error
(format "`next ~a` without for" 'LOOP-ID)))
(define func (hash-ref next-funcs 'LOOP-ID))
(func)))

@ -1,4 +0,0 @@
#lang info
(define compile-omit-paths '("private"))
(define test-omit-paths '("private" "sample-input.rkt"))

@ -1,31 +0,0 @@
#lang br
(require brag/support)
(define-lex-abbrev digits (:+ (char-set "0123456789")))
(define-lex-abbrev reserved-terms (:or "print" "goto" "end" "+" ":" ";" "let" "=" "input" "-" "*" "/" "^" "mod" "(" ")" "if" "then" "else" "<" ">" "<>" "and" "or" "not" "gosub" "return" "for" "to" "step" "next" "def" "," "import" "export"))
(define-lex-abbrev racket-id-kapu
(:or whitespace (char-set "()[]{}\",'`;#|\\")))
(define basic-lexer
(lexer-srcloc
["\n" (token 'NEWLINE lexeme)]
[whitespace (token lexeme #:skip? #t)]
[(from/stop-before "rem" "\n") (token 'REM lexeme)]
[(:seq "[" (:+ (:~ racket-id-kapu)) "]")
(token 'RACKET-ID
(string->symbol (trim-ends "[" lexeme "]")))]
[reserved-terms (token lexeme lexeme)]
[(:seq alphabetic (:* (:or alphabetic numeric "$")))
(token 'ID (string->symbol lexeme))]
[digits (token 'INTEGER (string->number lexeme))]
[(:or (:seq (:? digits) "." digits)
(:seq digits "."))
(token 'DECIMAL (string->number lexeme))]
[(:or (from/to "\"" "\"") (from/to "'" "'"))
(token 'STRING
(substring lexeme
1 (sub1 (string-length lexeme))))]))
(provide basic-lexer)

@ -1,22 +0,0 @@
#lang br
(require "struct.rkt")
(provide b-line raise-line-error)
(define-macro (b-line NUM STATEMENT ...)
(with-pattern ([LINE-NUM (prefix-id "line-" #'NUM
#:source #'NUM)])
(syntax/loc caller-stx
(define (LINE-NUM #:error [msg #f])
(with-handlers
([line-error?
(λ (le) (handle-line-error NUM le))])
(when msg (raise-line-error msg))
STATEMENT ...)))))
(define (raise-line-error error-msg)
(raise (line-error error-msg)))
(define (handle-line-error num le)
(error (format "error in line ~a: ~a"
num (line-error-msg le))))

@ -1,19 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(module+ reader
(provide read-syntax get-info))
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-mod basic-demo-3/expander
#,parse-tree)))
(define (get-info port src-mod src-line src-col src-pos)
(define (handle-query key default)
(case key
[(color-lexer)
(dynamic-require 'basic-demo-3/colorer 'basic-colorer)]
[else default]))
handle-query)

@ -1,34 +0,0 @@
#lang br
(require "struct.rkt" "expr.rkt")
(provide b-rem b-print b-let b-input b-import b-export b-repl)
(define (b-rem val) (void))
(define (b-print . vals)
(displayln (string-append* (map ~a vals))))
(define-macro (b-let ID VAL) #'(set! ID VAL))
(define-macro (b-input ID)
#'(b-let ID (let* ([str (read-line)]
[num (string->number (string-trim str))])
(or num str))))
(define-macro (b-import NAME) #'(void))
(define-macro (b-export NAME) #'(void))
(define-macro (b-repl . ALL-INPUTS)
(with-pattern
([INPUTS (pattern-case-filter #'ALL-INPUTS
[(b-print . PRINT-ARGS)
#'(b-print . PRINT-ARGS)]
[(b-expr . EXPR-ARGS)
#'(b-print (b-expr . EXPR-ARGS))]
[(b-let ID VAL)
#'(define ID VAL)]
[(b-def FUNC-ID VAR-ID ... EXPR)
#'(define (FUNC-ID VAR-ID ...) EXPR)]
[ANYTHING-ELSE
#'(error 'invalid-repl-input)])])
#'(begin . INPUTS)))

@ -1,14 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-parser-mod basic-demo-3/parse-only
#,parse-tree)))
(module+ reader (provide read-syntax))
(define-macro (parser-only-mb PARSE-TREE)
#'(#%module-begin
'PARSE-TREE))
(provide (rename-out [parser-only-mb #%module-begin]))

@ -1,15 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-parser-mod basic-demo-3/parse-stx
#'#,parse-tree)))
(module+ reader (provide read-syntax))
(define-macro (parser-only-mb PARSE-STX)
#'(#%module-begin
PARSE-STX))
(provide (rename-out [parser-only-mb #%module-begin]))
(provide syntax)

@ -1,40 +0,0 @@
#lang brag
b-program : [b-line] (/NEWLINE [b-line])* | b-statement /NEWLINE
b-line : b-line-num [b-statement] (/":" [b-statement])* [b-rem]
@b-line-num : INTEGER
b-rem : REM
@b-statement : b-end | b-print | b-goto
| b-let | b-input | b-if
| b-gosub | b-return | b-for | b-next
| b-def | b-import | b-export
b-end : /"end"
b-print : /"print" [b-printable] (/";" [b-printable])*
@b-printable : STRING | b-expr
b-goto : /"goto" b-expr
b-let : [/"let"] b-id /"=" (STRING | b-expr)
b-if : /"if" b-expr /"then" (b-statement | b-expr)
[/"else" (b-statement | b-expr)]
b-input : /"input" b-id
@b-id : ID
b-gosub : /"gosub" b-expr
b-return : /"return"
b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr]
b-next : /"next" b-id
b-def : /"def" b-id /"(" ID [/"," ID]* /")" /"=" b-expr
b-import : /"import" b-import-name
@b-import-name : RACKET-ID | STRING
b-export : /"export" b-export-name
@b-export-name : ID
b-expr : b-or-expr
b-or-expr : [b-or-expr "or"] b-and-expr
b-and-expr : [b-and-expr "and"] b-not-expr
b-not-expr : ["not"] b-comp-expr
b-comp-expr : [b-comp-expr ("="|"<"|">"|"<>")] b-sum
b-sum : [b-sum ("+"|"-")] b-product
b-product : [b-product ("*"|"/"|"mod")] b-neg
b-neg : ["-"] b-expt
b-expt : [b-expt ("^")] b-value
@b-value : b-number | b-id | /"(" b-expr /")" | b-func
b-func : (ID | RACKET-ID) /"(" b-expr [/"," b-expr]* /")"
@b-number : INTEGER | DECIMAL
b-repl : (b-statement | b-expr) (/":" [@b-repl])*

@ -1,8 +0,0 @@
(define (30) (rem print "'ignored'"))
(define (35) (void))
(define (50) (print "never gets here"))
(define (40) (end))
(define (60) (print "three") (print (+ 1.0 3)))
(define (70) (goto (+ 11 18.5 0.5)))
(define (10) (print "one"))
(define (20) (print) (goto 60) (end))

@ -1,5 +0,0 @@
#lang basic-demo-3
10 print "arg0 is " ; arg0
20 print "arg1 + arg1 is " ; arg1 + arg1
40 print "arg3 is " ; arg3
50 print "arg4 is " ; arg4

@ -1,23 +0,0 @@
#lang br
(require "line.rkt" "struct.rkt")
(provide run)
(define (run line-table)
(define line-vec
(list->vector (sort (hash-keys line-table) <)))
(with-handlers ([end-program-signal? (λ (exn-val) (void))])
(for/fold ([line-idx 0])
([i (in-naturals)]
#:break (>= line-idx (vector-length line-vec)))
(define line-num (vector-ref line-vec line-idx))
(define line-func (hash-ref line-table line-num))
(with-handlers
([change-line-signal?
(λ (cls)
(define clsv (change-line-signal-val cls))
(or
(and (exact-positive-integer? clsv)
(vector-member clsv line-vec))
(line-func #:error (format "line ~a not found" clsv))))])
(line-func)
(add1 line-idx)))))

@ -1,8 +0,0 @@
#lang basic-demo-3
10 rem all results should be 1
20 a = 5
30 b = 10
40 print a > 4
50 print b = 10
60 print b < 11
70 print rem (b = 100)

@ -1,7 +0,0 @@
#lang basic-demo-3
10 x = 2 : y = 3 : z = 5
20 print f(3, 4)
30 print f(f(3, g(2)), 2)
40 def f(x, y) = x * y * z
50 def g(i) = i + i
60 print y

@ -1,9 +0,0 @@
#lang basic-demo-3
10 def div(num, denom) = num / denom
20 x = 5 : y = 10
30 print x - 4
40 x = 15 : y = 30
50 print div(y, x)
60 x = 20
70 print div((x + x + x), x)
80 export div : export x

@ -1,6 +0,0 @@
#lang basic-demo-3
10 for a = 1 to 3
21 for b = 9 to 7 step -1
22 print a : print b
23 next b
30 next a

@ -1,10 +0,0 @@
#lang basic-demo-3
10 gosub 41
20 print "second"
30 gosub 100
31 print "fourth"
35 end
40 return
41 print "first" : return
100 print "third"
110 goto 40

@ -1,7 +0,0 @@
#lang basic-demo-3
10 import [math/number-theory]
20 print [nth-prime](15)
30 print [prime?](24)
40 import [racket/base]
50 print [max](f(1), f(2), f(5), f(4))
60 def f(x) = x + x

@ -1,5 +0,0 @@
#lang br
(require basic-demo-3/sample-exporter)
div
x
(div x 10)

@ -1,4 +0,0 @@
#lang basic-demo-3
5 print "enter your name: "
10 input A$
20 print "hello, " ; A$ ; "!"

@ -1,9 +0,0 @@
#lang basic-demo-3
10 rem all results should be 1
20 print 1 - 2 * 3 + 4 * 5 - 6 = 9
30 print (1 - 2) * (3 + 4) * (5 - 6) = 7
40 print 1 / 4 = .25
50 print 2 ^ 3 = 8
60 print 9 ^ 0.5 = 3
70 print 6 mod 2 = 0
80 print 5 mod 2 = 1

@ -1,6 +0,0 @@
#lang br
(require "sample-exporter.rkt")
(provide (all-from-out "sample-exporter.rkt"))
div
x
(div x 10)

@ -1,2 +0,0 @@
#lang basic-demo-3
10 print "got shell args: " ; arg0 ; arg1 ; arg2

@ -1,9 +0,0 @@
#lang basic-demo-3
30 rem print 'ignored'
35
50 print "never gets here"
40 end
60 print 'three' : print 1.0 + 3
70 goto 11. + 18.5 + .5 rem ignored
10 print "o" ; "n" ; "e"
20 print : goto 60.0 : end

@ -1,19 +0,0 @@
#lang br
(require "parser.rkt" "tokenizer.rkt")
(provide basic-output-port do-setup!)
(define basic-output-port
(make-parameter (open-output-nowhere)))
(define repl-parse (make-rule-parser b-repl))
(define (read-one-line origin port)
(define one-line (read-line port))
(if (eof-object? one-line)
eof
(repl-parse
(make-tokenizer (open-input-string one-line)))))
(define (do-setup!)
(basic-output-port (current-output-port))
(current-read-interaction read-one-line))

@ -1,8 +0,0 @@
#lang br
(provide (struct-out end-program-signal)
(struct-out change-line-signal)
(struct-out line-error))
(struct end-program-signal ())
(struct change-line-signal (val))
(struct line-error (msg))

@ -1,7 +0,0 @@
#lang br
(provide (all-defined-out))
(struct end-program-signal ())
(struct change-line-signal (val))
(struct line-error (msg))

@ -1,72 +0,0 @@
#lang at-exp br
(require br/test rackunit)
(check-equal? (run-source "sample.rkt") "one
three
4
")
(check-equal? (run-source "sample-cond.rkt") "1
1
1
")
(check-equal? (run-source "sample-def.rkt") "60
600
3
")
(check-equal? (run-source "sample-exporter.rkt") "1
2
3
")
(check-equal? (run-source "sample-for.rkt") "1
9
1
8
1
7
2
9
2
8
2
7
3
9
3
8
3
7
")
(check-equal? (run-source "sample-gosub.rkt") "first
second
third
fourth
")
(check-equal? (run-source "sample-import.rkt") "53
0
10
")
(check-equal? (run-source "sample-importer.rkt") "#<procedure:div>
20
2
")
(check-equal? (run-source "sample-math.rkt") "1
1
1
1
1
1
1
")
(check-equal? (run-source "sample-shell.rkt") "got shell args: 000\n")

@ -1,14 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt" brag/support)
(define (read-syntax path port)
(define tokens (apply-tokenizer-maker make-tokenizer port))
(strip-bindings
#`(module basic-parser-mod basic-demo-3/parse-only
'#,tokens)))
(module+ reader (provide read-syntax))
(define-macro (mb PARSE-TREE)
#'(#%module-begin
'PARSE-TREE))
(provide (rename-out [mb #%module-begin]))

@ -1,10 +0,0 @@
#lang br
(require "lexer.rkt" brag/support)
(define (make-tokenizer ip [path #f])
(port-count-lines! ip)
(lexer-file-path path)
(define (next-token) (basic-lexer ip))
next-token)
(provide make-tokenizer)

@ -10,6 +10,7 @@
(define basic-lexer (define basic-lexer
(lexer-src-pos (lexer-src-pos
[(eof) eof]
[whitespace (token 'WHITE lexeme #:skip? #t [whitespace (token 'WHITE lexeme #:skip? #t
#:position (pos lexeme-start) #:position (pos lexeme-start)
#:line (line lexeme-start) #:line (line lexeme-start)

@ -0,0 +1,7 @@
#lang basic-demo
10 rem what?
20 print "hello"
30 rem goto 50
45 print "shite"
50 print "foobar"
60 goto 45

@ -0,0 +1,51 @@
#lang br
(require brag/support syntax-color/racket-lexer racket/contract
basic-demo/tokenizer sugar/coerce)
(define (color-basic ip)
(define postok (basic-lexer ip))
(define tok (position-token-token postok))
(define-values (type val)
(cond
[(eof-object? tok) (values eof eof)]
[(string? tok) (values 'string tok)]
[else (values (token-struct-type tok)
(format "~a" (token-struct-val tok)))]))
(values val
(caseq type
[(WHITE) 'white-space]
[(COMMENT) 'comment]
[(NUMBER) 'constant]
[(STRING) 'string]
[else 'keyword])
#f
(position-offset (position-token-start-pos postok))
(position-offset (position-token-end-pos postok))))
#;(provide
(contract-out
[color-basic
(input-port? . -> . (values
(or/c string? eof-object?)
symbol?
(or/c symbol? #f)
(or/c exact-positive-integer? #f)
(or/c exact-positive-integer? #f)))]))
(define (apply-colorer colorer-proc str)
(let loop ([p (open-input-string str)][color-recs empty])
(define color-rec (values->list (colorer-proc p)))
(if (eof-object? (car color-rec))
(reverse color-recs)
(loop p (cons color-rec color-recs)))))
(module+ main
(define str #<<HERE
10 rem 20
20 x = 42
30 print x
HERE
)
(apply-colorer color-basic str))

@ -1,54 +1,46 @@
#lang br/quicklang #lang br/quicklang
(provide (matching-identifiers-out #rx"^b-" (all-defined-out)))
(define-macro (b-line NUM STATEMENT ...)
(with-pattern ([LINE-NUM (prefix-id "line-" #'NUM
#:source #'NUM)])
(syntax/loc caller-stx
(define (LINE-NUM) (void) STATEMENT ...))))
(define-macro (b-module-begin (b-program LINE ...)) (define-macro (b-module-begin (b-program LINE ...))
(with-pattern (with-pattern ([(LINE-NUM ...)
([((b-line NUM STMT ...) ...) #'(LINE ...)] (filter-stx-prop 'b-line-number (syntax-flatten #'(LINE ...)))]
[(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))]) [(LINE-ID ...) (syntax-map (λ (stx) (prefix-id "line-" stx)) #'(LINE-NUM ...))])
#'(#%module-begin #'(#%module-begin
LINE ... LINE ...
(define line-table (define line-table (apply hasheqv (append (list LINE-NUM LINE-ID) ...)))
(apply hasheqv (append (list NUM LINE-FUNC) ...))) (run line-table))))
(void (run line-table)))))
(provide (rename-out [b-module-begin #%module-begin])) (provide (rename-out [b-module-begin #%module-begin]))
(struct end-program-signal ()) (define-macro (b-line LINE-NUMBER STATEMENT)
(struct change-line-signal (val)) (with-pattern ([LINE-NUMBER-ID (prefix-id "line-" #'LINE-NUMBER
#:source #'LINE-NUMBER)]
[ORIG-LOC caller-stx])
(syntax/loc caller-stx (define (LINE-NUMBER-ID #:srcloc? [srcloc #f])
(if srcloc
(syntax-srcloc #'ORIG-LOC)
STATEMENT)))))
(define (b-statement stmt) stmt)
(define (b-rem str) #f)
(define (b-print str) (displayln str))
(define (b-goto line-number) line-number)
(define-exn end-program-signal exn:fail)
(define (b-end) (raise-end-program-signal))
(define (b-end) (raise (end-program-signal))) (provide b-line b-statement b-rem b-print b-goto b-end)
(define (b-goto expr) (raise (change-line-signal expr)))
(define-exn-srcloc line-not-found exn:fail)
(define (run line-table) (define (run line-table)
(define line-vec (define line-vec (list->vector (sort (hash-keys line-table) <)))
(list->vector (sort (hash-keys line-table) <))) (with-handlers ([end-program-signal? void])
(with-handlers ([end-program-signal? (λ (exn-val) (void))])
(for/fold ([line-idx 0]) (for/fold ([line-idx 0])
([i (in-naturals)] ([i (in-naturals)])
#:break (>= line-idx (vector-length line-vec))) (unless (< line-idx (vector-length line-vec)) (b-end))
(define line-num (vector-ref line-vec line-idx)) (define line-num (vector-ref line-vec line-idx))
(define line-func (hash-ref line-table line-num)) (define line-proc (hash-ref line-table line-num))
(with-handlers (define line-result (line-proc))
([change-line-signal? (if (exact-positive-integer? line-result)
(λ (cls) (or (vector-member line-result line-vec)
(define clsv (change-line-signal-val cls)) (raise-line-not-found (line-proc #:srcloc? #t)))
(or (add1 line-idx)))))
(and (exact-positive-integer? clsv)
(vector-member clsv line-vec))
(error
(format "error in line ~a: line ~a not found"
line-num clsv))))])
(line-func)
(add1 line-idx)))))
(define (b-rem val) (void))
(define (b-print . vals)
(displayln (string-append* (map ~a vals))))
(define (b-sum . vals) (apply + vals))
(define (b-expr expr)
(if (integer? expr) (inexact->exact expr) expr))

@ -0,0 +1,8 @@
#lang br
(provide (rename-out [my-app #%app]))
(define-macro (my-app ID . ARGS)
(report caller-stx)
(if (number? (syntax->datum #'ID))
(with-pattern ([NEW-ID (report* #'ID (prefix-id "@" #'ID))])
#'(NEW-ID . ARGS))
#'(ID . ARGS)))

@ -0,0 +1,2 @@
#lang br
(require "basic-test.rkt")

@ -1,4 +0,0 @@
#lang info
(define test-omit-paths '("private"))
(define compile-omit-paths '("private"))

@ -1,66 +0,0 @@
#lang br
(require "lexer.rkt" brag/support rackunit)
(define (lex str)
(apply-port-proc basic-lexer str))
(check-equal? (lex "") empty)
(check-equal?
(lex " ")
(list (srcloc-token (token " " #:skip? #t)
(srcloc 'string 1 0 1 1))))
(check-equal?
(lex "rem ignored\n")
(list (srcloc-token (token 'REM "rem ignored")
(srcloc 'string 1 0 1 11))
(srcloc-token (token 'NEWLINE "\n")
(srcloc 'string 1 11 12 1))))
(check-equal?
(lex "print")
(list (srcloc-token (token "print" "print")
(srcloc 'string 1 0 1 5))))
(check-equal?
(lex "goto")
(list (srcloc-token (token "goto" "goto")
(srcloc 'string 1 0 1 4))))
(check-equal?
(lex "end")
(list (srcloc-token (token "end" "end")
(srcloc 'string 1 0 1 3))))
(check-equal?
(lex "+")
(list (srcloc-token (token "+" "+")
(srcloc 'string 1 0 1 1))))
(check-equal?
(lex ";")
(list (srcloc-token (token ";" ";")
(srcloc 'string 1 0 1 1))))
(check-equal?
(lex ":")
(list (srcloc-token (token ":" ":")
(srcloc 'string 1 0 1 1))))
(check-equal?
(lex "12")
(list (srcloc-token (token 'INTEGER 12)
(srcloc 'string 1 0 1 2))))
(check-equal?
(lex "1.2")
(list (srcloc-token (token 'DECIMAL 1.2)
(srcloc 'string 1 0 1 3))))
(check-equal?
(lex "12.")
(list (srcloc-token (token 'DECIMAL 12.)
(srcloc 'string 1 0 1 3))))
(check-equal?
(lex ".12")
(list (srcloc-token (token 'DECIMAL .12)
(srcloc 'string 1 0 1 3))))
(check-equal?
(lex "\"foo\"")
(list (srcloc-token (token 'STRING "foo")
(srcloc 'string 1 0 1 5))))
(check-equal?
(lex "'foo'")
(list (srcloc-token (token 'STRING "foo")
(srcloc 'string 1 0 1 5))))
(check-exn exn:fail:read? (lambda () (lex "x")))

@ -1,22 +0,0 @@
#lang br
(require brag/support)
(define-lex-abbrev digits (:+ (char-set "0123456789")))
(define basic-lexer
(lexer-srcloc
["\n" (token 'NEWLINE lexeme)]
[whitespace (token lexeme #:skip? #t)]
[(from/stop-before "rem" "\n") (token 'REM lexeme)]
[(:or "print" "goto" "end"
"+" ":" ";") (token lexeme lexeme)]
[digits (token 'INTEGER (string->number lexeme))]
[(:or (:seq (:? digits) "." digits)
(:seq digits "."))
(token 'DECIMAL (string->number lexeme))]
[(:or (from/to "\"" "\"") (from/to "'" "'"))
(token 'STRING
(substring lexeme
1 (sub1 (string-length lexeme))))]))
(provide basic-lexer)

@ -1,11 +1,14 @@
#lang br/quicklang #lang br/quicklang
(require "parser.rkt" "tokenizer.rkt") (require "parser.rkt" "tokenizer.rkt")
(module+ reader (provide read-syntax))
(define (read-syntax path port) (define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path))) (define-values (line col pos) (port-next-location port))
(define port+newline (input-port-append #f port (open-input-string "\n")))
(port-count-lines! port+newline)
(set-port-next-location! port+newline line col pos)
(define parse-tree (parse path (tokenize port+newline)))
(strip-bindings (strip-bindings
#`(module basic-mod basic-demo/expander #`(module basic-mod basic-demo/expander
#,parse-tree))) #,parse-tree)))
(module+ reader
(provide read-syntax))

@ -1,14 +0,0 @@
#lang br/quicklang
(require "parser.rkt" "tokenizer.rkt")
(define (read-syntax path port)
(define parse-tree (parse path (make-tokenizer port path)))
(strip-bindings
#`(module basic-parser-mod basic-demo/parse-only
#,parse-tree)))
(module+ reader (provide read-syntax))
(define-macro (parser-only-mb PARSE-TREE)
#'(#%module-begin
'PARSE-TREE))
(provide (rename-out [parser-only-mb #%module-begin]))

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save