From f80a01a9c0da691973ac5cfce47ed4cd0c27eec9 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Tue, 14 Dec 2010 13:41:37 -0300 Subject: [PATCH] cameo: adding toolpath adaptation language (in progress) - README: added description of toolpath adaptation language (in progress) - Makefile (OBJS): added lex.yy.o and y.tab.o - Makefile (SLOPPY): relaxed compiler warnings for files generated by flex/yacc - Makefile (clean): added lex/yacc intermediate files - Makefile: added lex and yacc (bison) to build process - Makefile: added rules for lex/yacc intermediate files - lang.l, lang.y: toolpath adaptation language (in progress) --- cameo/Makefile | 36 +++++++++- cameo/README | 59 ++++++++++++++++ cameo/lang.l | 93 +++++++++++++++++++++++++ cameo/lang.y | 183 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 369 insertions(+), 2 deletions(-) create mode 100644 cameo/lang.l create mode 100644 cameo/lang.y diff --git a/cameo/Makefile b/cameo/Makefile index 2e0f659..e9b05c3 100644 --- a/cameo/Makefile +++ b/cameo/Makefile @@ -15,26 +15,45 @@ PREFIX ?= /usr/local SHELL=/bin/bash MAIN=cameo -OBJS=cameo.o gerber.o gnuplot.o path.o +OBJS=cameo.o gerber.o gnuplot.o path.o lex.yy.o y.tab.o CFLAGS_WARN=-Wall -Wshadow -Wmissing-prototypes \ -Wmissing-declarations -Wno-format-zero-length +SLOPPY = -Wno-unused -Wno-implicit-function-declaration + +# An older version of SLOPPY (seems that bison and flex got tidier): +# +#SLOPPY = -Wno-unused -Wno-implicit-function-declaration \ +# -Wno-missing-prototypes -Wno-missing-declarations +# + CFLAGS=$(CFLAGS_WARN) -g -LDFLAGS=-lm +LDFLAGS= +LDLIBS=-lm -lfl +YACC = bison -y +YYFLAGS = -v # ----- Verbosity control ----------------------------------------------------- CC_normal := $(CC) +YACC_normal := $(YACC) +LEX_normal := $(LEX) DEPEND_normal := $(CPP) $(CFLAGS) -MM -MG CC_quiet = @echo " CC " $@ && $(CC_normal) +YACC_quiet = @echo " YACC " $@ && $(YACC_normal) +LEX_quiet = @echo " LEX " $@ && $(LEX_normal) DEPEND_quiet = @$(DEPEND_normal) ifeq ($(V),1) CC = $(CC_normal) + LEX = $(LEX_normal) + YACC = $(YACC_normal) DEPEND = $(DEPEND_normal) else CC = $(CC_quiet) + LEX = $(LEX_quiet) + YACC = $(YACC_quiet) DEPEND = $(DEPEND_quiet) endif @@ -46,8 +65,21 @@ all: $(MAIN) $(MAIN): $(OBJS) +lex.yy.c: lang.l y.tab.h + $(LEX) lang.l + +lex.yy.o: lex.yy.c y.tab.h + $(CC) -c $(CFLAGS) $(SLOPPY) lex.yy.c + +y.tab.c y.tab.h: lang.y + $(YACC) $(YYFLAGS) -d lang.y + +y.tab.o: y.tab.c y.tab.h + $(CC) -c $(CFLAGS) $(SLOPPY) y.tab.c + clean: rm -f $(OBJS) $(OBJS:.o=.d) + rm -f lex.yy.c y.output y.tab.c y.tab.h spotless: clean rm -f $(MAIN) diff --git a/cameo/README b/cameo/README index 4cb1118..a953f43 100644 --- a/cameo/README +++ b/cameo/README @@ -19,3 +19,62 @@ LICENSE, Version 2: For your convenience, a copy of the complete license has been included in the file COPYING.GPLv2. + + +Toolpath adaptation language +---------------------------- + +Loading paths: + + gerber [] + gnuplot [] + clear + +"gerber" and "gnuplot" each add paths from a file to the list of paths +already loaded. "clear" removes all paths. + +If the a file name is omitted, standard input is read. The diameter is +the default tool diameter. + +Note that "clear" preserves the translation set with "align", "array", +and "translate". This way, multiple files can be processed with the same +translation. + +File output: + + write [] + +Writes all loaded paths in gnuplot format to the specified file. If the +file name is omitted, "write" writes to standard output. + +Units: + + mm + mil + mm + mil + +The words "mm" and "mil" set the input to the specified unit. The +default unit is millimeter. If a number is followed by "mm" or "mil" +without intervening space, the unit is overridden for this number, +without affecting the default input unit. + +Note that "mm" and "mil" only affect dimensions in the command file +and have no effect on the interpretation of toolpath files. + + +Horizontal adjustments: + + align + align + array + translate + reset + +Vertical adjustment: + + z [] + +Tool compensation: + + tool diff --git a/cameo/lang.l b/cameo/lang.l new file mode 100644 index 0000000..479d109 --- /dev/null +++ b/cameo/lang.l @@ -0,0 +1,93 @@ +%{ +/* + * lang.l - Toolpath adaptation language + * + * Written 2010 by Werner Almesberger + * Copyright 2010 by Werner Almesberger + * + * This program 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 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include + +#include "y.tab.h" + + +void yyerror(const char *s); + +static int lineno = 1; +static int metric = 1; + +#define MIL2MM(mil) ((mil)/1000*25.4) + +%} + +/* file name can contain any characters */ +%s FILENAME + +/* "align" has a one-digit argument we don't want to subject to unit + conversion */ +%s DIGIT + +NUM [0-9]+\.?[0-9]* + +%% + + +align { BEGIN(DIGIT); + return TOK_ALIGN; } +array return TOK_ARRAY; +clear return TOK_CLEAR; +reset return TOK_RESET; +tool return TOK_TOOL; +translate return TOK_TRANSLATE; +z return TOK_Z; + +gerber { BEGIN(FILENAME); + return TOK_GERBER; } +gnuplot { BEGIN(FILENAME); + return TOK_GNUPLOT; } +write { BEGIN(FILENAME); + return TOK_WRITE; } + +mm metric = 1; +mil metric = 0; + +{NUM}mm { yylval.num = strtod(yytext, NULL); + return NUMBER; } +{NUM}mil { yylval.num = MIL2MM(strtod(yytext, NULL)); + return NUMBER; } +{NUM} { yylval.num = strtod(yytext, NULL); + if (!metric) + yylval.num = MIL2MM(yylval.num); + return NUMBER; } + +[^ \t\n]+([^\t\n]*[^ \t\n]+)? { + BEGIN(INITIAL); + yylval.str = yytext; + return STRING; } + +[1-9] { BEGIN(INITIAL); + yylval.num = strtod(yytext, NULL); + return NUMBER; } + +[ \t] ; +\n lineno++; + +^#\ [0-9]+\ \"[^"]*\"(\ [0-9]+)*\n { + lineno = strtol(yytext+2, NULL, 0); } + +. return *yytext; + +%% + +void yyerror(const char *s) +{ + fprintf(stderr, "%d: %s near \"%s\"\n", lineno, s, yytext); + exit(1); +} diff --git a/cameo/lang.y b/cameo/lang.y new file mode 100644 index 0000000..0bdd616 --- /dev/null +++ b/cameo/lang.y @@ -0,0 +1,183 @@ +%{ +/* + * lang.y - Toolpath adaptation language + * + * Written 2010 by Werner Almesberger + * Copyright 2010 by Werner Almesberger + * + * This program 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 2 of the License, or + * (at your option) any later version. + */ + + +#include + +#include "path.h" +#include "gnuplot.h" +#include "gerber.h" + +#include "y.tab.h" + + +static double xo = 0, yo = 0, zo = 0; +static struct path *paths = NULL; + + +static void add_paths(struct path *new) +{ + struct path **anchor = &paths; + + while (*anchor) + anchor = &(*anchor)->next; + *anchor = new; +} + + +static void translate(double x, double y, double z) +{ + struct path *path; + struct point *p; + + for (path = paths; path; path = path->next) + for (p = path->first; p; p = p->next) { + p->x += x; + p->y += y; + p->z += z; + } +} + + +static double ref_pick_1(int ref, double a, double b) +{ + switch (ref) { + case 0: + return a; + case 1: + return (a+b)/2; + case 2: + return b; + default: + abort(); + } +} + + +static void ref_pick_xy(int ref, double xa, double ya, double xb, double yb, + double *x, double *y) +{ + *x = ref_pick_1((ref-1) % 3, xa, xb); + *y = ref_pick_1((ref-1)/3, ya, yb); +} + + +static void align(int ref, double x, double y) +{ + /* @@@ */ +} + + +%} + + +%union { + double num; + char *str; +}; + + +%token TOK_ALIGN TOK_ARRAY TOK_CLEAR TOK_RESET TOK_TOOL +%token TOK_TRANSLATE TOK_Z +%token TOK_GERBER TOK_GNUPLOT TOK_WRITE + +%token NUMBER +%token STRING + +%type opt_filename + +%% + +all: + | command all + ; + +command: + TOK_ALIGN NUMBER NUMBER NUMBER + { + align((int) $2, $3, $4); + } + | TOK_ALIGN NUMBER NUMBER NUMBER NUMBER NUMBER + { + int ref = $2; + double x, y; + + ref_pick_xy(ref, $3, $4, $5, $6, &x, &y); + align(ref, x, y); + } + | TOK_ARRAY NUMBER NUMBER NUMBER NUMBER + { + double x = $2*$4; + double y = $3*$5; + + translate(x, y, 0); + } + | TOK_CLEAR + { + struct path *next; + + while (paths) { + next = paths->next; + path_free(paths); + paths = next; + } + } + | TOK_RESET + { + xo = yo = 0; + } + | TOK_TOOL + { + // @@@ process_paths(paths); + } + | TOK_TRANSLATE NUMBER NUMBER + { + translate($2, $3, 0); + xo += $2; + yo += $3; + } + | TOK_Z NUMBER + { + zo -= $2; + } + | TOK_Z NUMBER NUMBER + { + zo -= $2+$3; + } + | TOK_GERBER NUMBER opt_filename + { + add_paths(gerber_read($3, $2/2)); + translate(xo, yo, 0); + } + | TOK_GNUPLOT NUMBER opt_filename + { + add_paths(gnuplot_read($3, $2/2)); + translate(xo, yo, 0); + } + | TOK_WRITE opt_filename + { + translate(0, 0, zo); + gnuplot_write($2, paths); + translate(0, 0, -zo); + } + ; + +opt_filename: + { + $$ = NULL; + } + | STRING + { + $$ = $1; + } + ;