btcs

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

commit 3ce6229b04b88385068ae423e4c9b816c00ca78d
parent de9523caaf8b7b7a475e1a4cb3427bbf2827f629
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  4 Dec 2017 14:12:51 -0800

string literals

example
-------

  $ btcs <<<'return "hello, world"'
  script      OP_RETURN @68656c6c6f2c20776f726c64
  script_hex  6a0c68656c6c6f2c20776f726c64
  output
  output_hex

Diffstat:
Mlexer.l | 7+++++++
Mparser.y | 3+++
Mscript.c | 20++++++++++++++++++++
3 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/lexer.l b/lexer.l @@ -23,6 +23,13 @@ [oO][pP]_ {} +\".*\" { + char *t = yytext + 1; + t[strlen(t) - 1] = '\0'; + yylval.str = t; + return T_STR; +} + @[a-fA-F0-9]+ { if (strlen(yytext + 1) % 2 != 0) { yylval.str = "Invalid data string, byte string must have even length"; diff --git a/parser.y b/parser.y @@ -22,12 +22,14 @@ void yyerror(const char* s); %token T_INT %token T_VAL %token T_DATA +%token T_STR %token T_ERR %token T_NEWLINE T_QUIT T_EXAMPLE %type<opcode> T_OP %type<integer> T_INT %type<str> T_DATA +%type<str> T_STR %type<val> T_VAL %type<str> T_EXAMPLE %type<str> T_ERR @@ -45,6 +47,7 @@ line: T_NEWLINE | T_VAL { stack_push_val(&g_reader_stack, $1); } | T_OP { stack_push_op(&g_reader_stack, $1); } | T_DATA { script_push_datastr(&g_reader_stack, $1); } + | T_STR { script_push_str(&g_reader_stack, $1); } | T_ERR { parse_error($1); } | T_EXAMPLE { ; } diff --git a/script.c b/script.c @@ -225,6 +225,12 @@ script_eval(u8 *script, size_t script_size, struct stack *stack) { break; + case OP_RETURN: + { + return SCRIPTERR("OP_RETURN"); + } + break; + case OP_INVALIDOPCODE: { return SCRIPTERR("SCRIPT_ERR_INVALID_OPCODE"); @@ -691,6 +697,20 @@ script_push_int(struct stack *script, s64 intval) { void +script_push_str(struct stack *script, char *str) { + struct val val; + u16 ind; + u8 *bytes; + bytes = byte_pool_new(strlen(str), &ind); + strcpy((char*)bytes, str); + + val.type = VT_DATA; + val.ind = ind; + + stack_push_val(script, val); +} + +void script_push_datastr(struct stack *script, char *str) { int count = 0; u8 *bytes;