btcs

bitcoin script parser/evaluator/compiler/decompiler
git clone git://jb55.com/btcs
Log | Files | Refs | README | LICENSE

commit 49461b53cb1982b5329dcce71ec0c1d8b8cc0e8c
parent 73c07bcb6ee2641f2243afd27ab44a022f88fff9
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 25 Oct 2017 11:42:31 -0700

parser: split main into its own file

Diffstat:
MMakefile | 16++++++++++++----
Mlexer.l | 3+--
Amain.c | 41+++++++++++++++++++++++++++++++++++++++++
Mop.h | 10+++++++---
Mparser.y | 35++++++-----------------------------
Mscript.h | 18++++++------------
6 files changed, 73 insertions(+), 50 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,9 +1,17 @@ -CFLAGS=-O2 +CFLAGS=-O2 -Wall -Wno-unused-variable -GEN=parser.tab.c parser.tab.h lex.yy.c oplookup.c oplookup.o script.o oplookup.h -FORBIN=script.o parser.tab.c lex.yy.c oplookup.o op.o -DEPS=oplookup.h script.h misc.h Makefile op.h +FORBIN=script.o \ + parser.tab.o \ + lex.yy.o \ + oplookup.o \ + op.o \ + main.o \ + stack.o + +GEN=parser.tab.c parser.tab.h lex.yy.c oplookup.c oplookup.h $(FORBIN) + +DEPS=oplookup.h script.h misc.h Makefile op.h stack.h PREFIX ?= /usr/local BIN=btcs diff --git a/lexer.l b/lexer.l @@ -3,10 +3,10 @@ %{ #include <stdio.h> #include "script.h" +#include "op.h" #define YY_DECL int yylex() - #include "parser.tab.h" %} @@ -21,7 +21,6 @@ \[[^\]]+\] { yylval.str = yytext; return T_EXAMPLE; } - [oO][pP]_ {} [a-zA-Z0-9]+ { diff --git a/main.c b/main.c @@ -0,0 +1,41 @@ + +#include <stdio.h> +#include <stdlib.h> +#include "stack.h" +#include "script.h" + +extern int yyparse(); +extern FILE* yyin; + +struct stack reader_stack; + +void yyerror(const char* s); + +int main() { + yyin = stdin; + + struct stack tmp_stack; + stack_init(&reader_stack); + stack_init(&tmp_stack); + + do { + yyparse(); + } while(!feof(yyin)); + + script_eval(&reader_stack, &tmp_stack); + printf("script: "); + script_print(&reader_stack); + printf("stack: "); + script_print(&tmp_stack); + + stack_free(&reader_stack); + stack_free(&tmp_stack); + + return 0; +} + +void yyerror(const char* s) { + fprintf(stderr, "Parse error: %s\n", s); + exit(1); +} + diff --git a/op.h b/op.h @@ -1,6 +1,7 @@ -#ifndef BCS_SCRIPT_H -#define BCS_SCRIPT_H +#ifndef BCS_OP_H +#define BCS_OP_H + enum opcode { @@ -295,8 +296,11 @@ enum opcode_token _OP_INVALIDOPCODE, }; +// Maximum value that an opcode can be +static const unsigned int MAX_OPCODE = OP_NOP10; + void op_add(enum opcode); const char * op_name(enum opcode); enum opcode op_tokenize(char *); -#endif /* BCS_SCRIPT_H */ +#endif /* BCS_OP_H */ diff --git a/parser.y b/parser.y @@ -1,17 +1,13 @@ %{ - #include <stdio.h> -#include <stdlib.h> -#include "script.h" +#include "op.h" +#include "stack.h" extern int yylex(); -extern int yyparse(); -extern FILE* yyin; - -char buffer[255]; - +extern struct stack reader_stack; void yyerror(const char* s); + %} %union { @@ -34,27 +30,8 @@ script: ; line: T_NEWLINE - | T_OP { printf("%s\n", op_name($1)); } - | T_EXAMPLE { printf("ex:%s\n", $1); } - -/* opcode: */ -/* | T_OP { op_add($$); } */ -/* ; */ + | T_OP { stack_push(&reader_stack, $1); } + | T_EXAMPLE { ; } %% - -int main() { - yyin = stdin; - - do { - yyparse(); - } while(!feof(yyin)); - - return 0; -} - -void yyerror(const char* s) { - fprintf(stderr, "Parse error: %s\n", s); - exit(1); -} diff --git a/script.h b/script.h @@ -2,7 +2,7 @@ #ifndef BTCS_SCRIPT_H #define BTCS_SCRIPT_H -#include "op.h" +#include "stack.h" // Maximum number of bytes pushable to the stack static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; @@ -16,24 +16,18 @@ static const int MAX_PUBKEYS_PER_MULTISIG = 20; // Maximum script length in bytes static const int MAX_SCRIPT_SIZE = 10000; -// Maximum number of values on script interpreter stack -static const int MAX_STACK_SIZE = 1000; - -// Maximum value that an opcode can be -static const unsigned int MAX_OPCODE = OP_NOP10; - // Threshold for nLockTime: below this value it is interpreted as block number, // otherwise as UNIX timestamp. static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC struct script { + struct stack data; }; -struct script_state { -}; - -void -script_eval(struct script *, struct script_state*); +int script_new(struct script *); +void script_free(struct script *); +void script_eval(struct stack *, struct stack*); +void script_print(struct stack *); #endif /* BTCS_SCRIPT_H */