From 923df895c5019d161e7b7e87445848767ec178ec Mon Sep 17 00:00:00 2001 From: Nala Ginrut Date: Mon, 21 Jan 2013 18:34:51 +0800 Subject: [PATCH] First commit --- .gitignore | 64 ++++++++++++++++++++++++++++++++ csv/compile-tree-il.scm | 82 +++++++++++++++++++++++++++++++++++++++++ csv/lexer.scm | 52 ++++++++++++++++++++++++++ csv/parser.scm | 49 ++++++++++++++++++++++++ csv/spec.scm | 35 ++++++++++++++++++ 5 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 csv/compile-tree-il.scm create mode 100644 csv/lexer.scm create mode 100644 csv/parser.scm create mode 100644 csv/spec.scm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7e5ae7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,64 @@ +config.guess +config.sub +config.h.in +configure +obj/* +*.so +*.o +Makefile +.deps +.libs +autom4te.cache +config.h +*.doc +*.x +*.lo +*.la +aclocal.m4 +depcomp +missing +mdate-sh +texinfo.tex +*~ +,* +aclocal.m4 +confdefs.h +config.build-subdirs +config.cache +config.h +config.status +conftest +conftest.c +depcomp +ltconfig +mdate-sh +missing +mkinstalldirs +stamp-h1 +*.go +cscope.out +cscope.files +*.log +*.aux +*.cp +*.cps +*.dvi +*.fn +*.fns +*.ky +*.pg +*.toc +*.tp +*.vr +*.tps +*.vrs +*.pgs +*.rn +*.rns +*.scan +*.am +*.d +version.scm +*.m4 +ltmain.sh +libtool \ No newline at end of file diff --git a/csv/compile-tree-il.scm b/csv/compile-tree-il.scm new file mode 100644 index 0000000..0fcd600 --- /dev/null +++ b/csv/compile-tree-il.scm @@ -0,0 +1,82 @@ +;; Copyright (C) 2013 +;; "Mu Lei" known as "NalaGinrut" +;; This file is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +(define-module (language csv compile-tree-il) + #:use-module (language tree-il) + #:use-module (ice-9 receive) + #:use-module (system base pmatch) + #:use-module (srfi srfi-1) + #:export (compile-tree-il)) + +(define-syntax-rule (-> (type arg ...)) + `(type ,arg ...)) + +(define empty-lexical-environment + (make-hash-table)) + +(define (csv-init) + #t + ;; nothing to do yet. + ) + +(define (compile-tree-il exp env opts) + (values + (parse-tree-il + (begin (imp-init) + (comp exp empty-lexical-environment))) + env + env)) + +(define (location x) + (and (pair? x) + (let ((props (source-properties x))) + (and (not (null? props)) + props)))) + +(define-syntax-rule (->boolean x) + (not (eq? x 'false))) + +;; for emacs: +;; (put 'pmatch/source 'scheme-indent-function 1) + +(define-syntax-rule (pmatch/source x clause ...) + (let ((x x)) + (let ((res (pmatch x + clause ...))) + (let ((loc (location x))) + (if loc + (set-source-properties! res (location x)))) + res))) + +(define (lookup name env) + (hash-ref env name)) + +(define (store name value env) + (hash-set! env name value)) + +(define (comp src e) + (let ((l 0));;(location src))) + (define (let1 what proc) + (let ((sym (gensym))) + (-> (let (list sym) (list sym) (list what) + (proc sym))))) + (define (begin1 what proc) + (let1 what (lambda (v) + (-> (begin (proc v) + (-> (lexical v v))))))) + (pmatch/source + src + ))) + diff --git a/csv/lexer.scm b/csv/lexer.scm new file mode 100644 index 0000000..2e7a946 --- /dev/null +++ b/csv/lexer.scm @@ -0,0 +1,52 @@ +;; Copyright (C) 2012 +;; "Mu Lei" known as "NalaGinrut" +;; This file is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +(define-module (language csv lexer) + #:use-module (system base lalr) + #:use-module (ice-9 receive) + #:use-module (ice-9 rdelim) + #:export (make-csv-tokenizer)) + + +(define (port-source-location port) + (make-source-location (port-filename port) + (port-line port) + (port-column port) + (false-if-exception (ftell port)) + #f)) + +(define imp-tokenizer + (lambda (port) + (let* ((loc (port-source-location port)) + (return (lambda (category value) + (make-lexical-token category loc value))) + (c (peek-char port))) + (cond + ((eof-object? c) '*eoi*) + ;; TODO + (else + (error "wrong syntax!" c)))))) + +(define (make-imp-tokenizer port) + (lambda () + (imp-tokenizer port))) + +;; (define imp-tokenize +;; (lambda (port) +;; (let lp ((out '())) +;; (let ((tok (imp-tokenizer port))) +;; (if (eq? tok '*eoi*) +;; (reverse! out) +;; (lp (cons tok out))))))) diff --git a/csv/parser.scm b/csv/parser.scm new file mode 100644 index 0000000..030a37f --- /dev/null +++ b/csv/parser.scm @@ -0,0 +1,49 @@ +;; Copyright (C) 2013 +;; "Mu Lei" known as "NalaGinrut" +;; This file is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +(define-module (language csv parser) + #:use-module (language csv lexer) + #:use-module (system base lalr) + #:export (csv-read)) + +(define* (syntax-error message #:optional token) + (if (lexical-token? token) + (throw 'syntax-error #f message + (and=> (lexical-token-source token) + source-location->source-properties) + (or (lexical-token-value token) + (lexical-token-category token)) + #f) + (throw 'syntax-error #f message #f token #f))) + +(define *eof-object* + (call-with-input-string "" read-char)) + +(define (csv-read port) + (let ((parse (make-parser))) + (parse (make-csv-tokenizer port) syntax-error))) + +(define (make-parser) + (lalr-parser + (;; keyword + ;; TODO) + + (program (stmt) : $1 + (*eoi*) : *eof-object*) + ))) + + + + diff --git a/csv/spec.scm b/csv/spec.scm new file mode 100644 index 0000000..db088a5 --- /dev/null +++ b/csv/spec.scm @@ -0,0 +1,35 @@ +;; Copyright (C) 2013 +;; "Mu Lei" known as "NalaGinrut" +;; This file is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +(define-module (language csv spec) + #:use-module (system base language) + #:use-module (language csv compile-tree-il) + #:use-module (language csv parser) + #:export (csv)) + +;;; +;;; Language definition +;;; + +;; Actually, CSV is not a language anyway, but we define this seudo language to +;; convert CSV format into s-expr. +(define-language csv + #:title "CSV language" + #:reader (lambda (port env) + (csv-read port)) + #:compilers `((tree-il . ,compile-tree-il)) + #:printer write + ) +