begin implementing callback mode

This commit is contained in:
xenia 2020-11-07 15:53:00 -05:00
parent 57e2dfe29c
commit 0610a472ea
3 changed files with 32 additions and 9 deletions

View File

@ -1,6 +1,6 @@
#lang racket/base
(require racket/runtime-path
(require racket/match racket/runtime-path
scribble/text (rename-in scribble/text/output [output scribble-output]))
(provide pattern-codegen)
@ -11,8 +11,9 @@
(define cs (current-namespace))
(define output-exp
(parameterize ([current-namespace (make-base-namespace)])
(namespace-attach-module cs 'scribble/text)
(namespace-require 'scribble/text)
(for ([mod (in-list '(scribble/text racket/match))])
(namespace-attach-module cs mod)
(namespace-require mod))
(hash-for-each vars namespace-set-variable-value!)
(eval `(include/text ,file))))
(scribble-output output-exp port))
@ -23,9 +24,10 @@
;; ok gamer move time
(define-runtime-path codegen-template "codegen.template")
(define (pattern-codegen pattern pp-start pp-end)
(define (pattern-codegen pattern mode pp-start pp-end)
(eval-template
`(file ,(path->string codegen-template))
(hash 'pattern pattern
'mode mode
'pp-start pp-start
'pp-end pp-end)))

View File

@ -3,11 +3,13 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
@(define c-type "uint64_t")
@(define c-type-fmt "%lx")
typedef @c-type vartype;
typedef bool (*callback)( @(add-between (for/list ([_ (in-vector pattern)]) "vartype") ",") );
typedef struct {
vartype start;
@ -32,7 +34,7 @@ typedef struct {
@(for/list ([i (in-naturals)] [iset (in-vector pattern)])
(output-iset i iset))
int main() {
int inputgen_main(callback cb) {
char buf [ @(* 20 (vector-length pattern)) ];
@(for/list ([num (in-naturals)] [iset-pos (in-vector pp-start)])
@list{size_t @(format "i~a" num) = @(car iset-pos) ;
@ -54,6 +56,8 @@ int main() {
@(define vs
(string-join (for/list ([i (in-range (vector-length pattern))]) (format "v~a" i)) ","))
@(define arg-vs
(string-join (for/list ([i (in-range (vector-length pattern))]) (format "vartype v~a" i)) ","))
@(define fmt
(string-join (for/list ([i (in-range (vector-length pattern))]) c-type-fmt) " "))
l_inner:
@ -70,10 +74,27 @@ l_inner:
goto l_end;
}
ssize_t res = snprintf(buf, sizeof(buf), @(format "\"~a\\n\"" fmt), @vs );
fwrite(buf, res, 1, stdout);
@(match mode
['stdout
@list{ssize_t res = snprintf(buf, sizeof(buf), @(format "\"~a\\n\"" fmt), @vs );
fwrite(buf, res, 1, stdout);}]
['callback
@list{ if (cb( @vs )) { report_success( @vs ); return 0; } }])
@(for/list ([iset (in-vector pattern)]) "}}")
l_end:
return 0;
}
@(if (equal? mode 'stdout)
@list{
int main() {
return inputgen_main(NULL);
}
}
@list{
void report_success( @arg-vs ) {
// TODO
}
})

View File

@ -67,9 +67,9 @@
(define-values [name mode command pattern]
(parse-manifest
'((name "test")
(mode stdin)
(mode callback)
(command ("meme"))
(pattern "test?d?a?a?s"))))
(pattern-codegen pattern (pattern-start pattern) (pattern-end pattern))
(pattern-codegen pattern mode (pattern-start pattern) (pattern-end pattern))
(printf "// total: ~a\n" (pattern-count pattern))