codegen C to generate candidates

This commit is contained in:
xenia 2020-10-25 02:40:31 -04:00
parent 126de096be
commit 2902931255
2 changed files with 58 additions and 4 deletions

35
codegen.template Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@(define (output-charset num cset)
@list{
unsigned char @(format "cset~a" num) [] = {
@(string-join (map number->string cset) ",")
};
size_t @(format "cset~a_size" num) = @(length cset);
})
@(for/list ([i (in-naturals)] [cset (in-vector pattern)])
(output-charset i cset))
int main() {
char buf [ @(+ 2 (vector-length pattern)) ];
buf [ @(vector-length pattern) ] = '\n';
buf [ @(add1 (vector-length pattern)) ] = '\0';
@(for/list ([num (in-naturals)] [cset (in-vector pattern)])
(define iter (format "i~a" num))
(define cset (format "cset~a" num))
(define csetsize (format "cset~a_size" num))
@list{
for (size_t @iter = 0; @iter < @csetsize ; @iter ++) @"{"
@"buf[" @num @"]" = @cset @"[" @iter @"];"
@"\n"
})
fwrite(buf, 1, @(add1 (vector-length pattern)), stdout);
@(for/list ([cset (in-vector pattern)]) "}")
}

View File

@ -1,6 +1,8 @@
#lang racket/base
(require racket/bool racket/function racket/match racket/set racket/vector
(rename-in scribble/text/output [output scribble-output])
scribble/text
(for-syntax racket/base racket/syntax))
(module+ test
@ -17,7 +19,20 @@
#:once-each
[("-n" "--name") name "Who to say hello to" (set-box! who name)]
#:args ()
(printf "hello ~a~n" (unbox who))))
(void)))
;; templating infrastructure
(define (eval-template file vars [port (current-output-port)])
(define cs (current-namespace))
(define output-exp
(parameterize ([current-namespace (make-base-namespace)])
(namespace-attach-module cs 'scribble/text)
(namespace-require 'scribble/text)
(hash-for-each vars namespace-set-variable-value!)
(eval `(include/text ,file))))
(scribble-output output-exp port))
;; manifest.rkt processing
@ -99,12 +114,16 @@
(permute (add1 i) gen))]))
(permute 0 gen))
;; ok gamer move time
(define (pattern-codegen pattern)
(eval-template "codegen.template" (hash 'pattern pattern)))
(define-values [name mode command pattern]
(parse-manifest
'((name "test")
(mode stdin)
(command ("meme"))
(pattern "test?dx?d"))))
(pattern "test?d?a?a?a"))))
(displayln (pattern-count pattern))
(pattern-generate pattern (current-output-port))
; (pattern-generate pattern (current-output-port))
(pattern-codegen pattern)