• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版3ee4ecc4571ee60e563341963a49c9a7f8c64652 (tree)
时间2021-08-11 02:02:54
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Redo and simplify argument handling.

Lists of lists should no longer be off the menu.

更改概述

差异

--- a/finish.py
+++ b/finish.py
@@ -2,18 +2,17 @@
22
33 import sys
44
5-parsers = {
6- "unit": "string->unit",
7- "bool": "string->boolean",
8- "nat": "string->nat",
9- "nat * nat": "string->int",
5+aliases = {
6+ "nat * nat": "int",
107 }
118
129 def get_parser(ty):
1310 if ty.endswith(" list"):
14- return "(string->listof {})".format(get_parser(ty[:-5]))
11+ return "(arg-list {})".format(get_parser(ty[:-5]))
12+ elif ty in aliases:
13+ return get_parser(aliases[ty])
1514 else:
16- return parsers[ty]
15+ return "arg-" + ty
1716
1817 with open(sys.argv[-1], "r", encoding="utf-8") as handle:
1918 line = handle.readlines()[-2]
@@ -32,7 +31,9 @@ with open(sys.argv[-2], "r", encoding="utf-8") as handle:
3231 print("""
3332 (import (chicken process-context))
3433 (define parsers (list {}))
35-(define parsed-args (zip-app parsers (command-line-arguments)))
34+(define parsed-args
35+ (map (lambda (arg p) (p (read-string arg)))
36+ (command-line-arguments) parsers))
3637 (begin
3738 (display (fold-left program (lambda (x f) (f x)) parsed-args))
3839 (newline))
--- a/stub.scm
+++ b/stub.scm
@@ -1,4 +1,6 @@
1+(import (srfi 6))
12 (import (chicken condition))
3+(import (chicken format))
24 (import (chicken string))
35
46 (define id (lambda (x) x))
@@ -31,24 +33,14 @@
3133 (define (fold x f)
3234 (lambda (l) (if (null? l) (x '()) (f (cons (car l) ((fold x f) (cdr l)))))))
3335
34-(define (parse-error message) (signal (condition (list 'exn 'message message))))
35-(define (string->unit s) '())
36-(define (string->boolean s) (equal? s "true"))
37-(define (string->nat s)
38- (let ((i (string->number s)))
39- (if (< i 0) (parse-error "nats must be positive")
40- i)))
41-(define (string->int s)
42- (let ((i (string->number s))) (if (< i 0) (cons 0 (abs i)) (cons i 0))))
43-(define (string->listof p) (lambda (s)
44- (let ((len (string-length s)))
45- (if (not (equal? #\( (string-ref s 0)))
46- (parse-error "list must start with ("))
47- (if (not (equal? #\) (string-ref s (- len 1))))
48- (parse-error "list must end with )"))
49- (map p (string-split (substring s 1 (- len 1)))))))
36+(define (read-string s) (read (open-input-string s)))
37+(define (arg-error arg why)
38+ (signal (condition (list 'exn 'message (sprintf "Invalid argument ~A: ~A" arg why)))))
39+(define (arg-unit x) '())
40+(define (arg-bool x) (if (boolean? x) x (arg-error x "not bool")))
41+(define (arg-nat x) (if (> x 0) x (arg-error x "not a natural number")))
42+(define (arg-int x)
43+ (if (number? x) (if (> x 0) (cons x 0) (cons 0 (abs x))) (arg-error x "not an integer")))
44+(define (arg-list p) (lambda (x) (map p x)))
5045
5146 (define (fold-left kn kc l) (if (null? l) kn (kc (car l) (fold-left kn kc (cdr l)))))
52-(define (zip-app fs xs)
53- (if (null? fs) '()
54- (cons ((car fs) (car xs)) (zip-app (cdr fs) (cdr xs)))))