git » unicorn-sparkle-basic.git » master » tree

[master] / usbasic.y

/*
 * This file is part of Unicorn Sparkle Basic and is released under
 * CC0 1.0 Universal License.  See LICENSE.txt file or
 * https://creativecommons.org/publicdomain/zero/1.0/ for full license
 * text.
 */

%{
#include <stdio.h>
#include <stdlib.h>
#include "parse.h"
extern int yylex();
extern int yyparse();
extern FILE *yyin;

void yyerror(const char *s) {
    fprintf(stderr, "Error: %s\n", s);
}
%}

%union {
  double number;
  char *id;
  char *str;
  struct node_tag *node;
}

%token <number> NUMBER
%token <id> IDENTIFIER
%token <str> STRING
%token RUN PRINT IF THEN GOTO LT LTE GT GTE EQ

%type <node> line
%type <node> expression
%type <node> statement

%define parse.error verbose

%left '+' '-'
%left '*' '/'

%%
line:
    NUMBER statement {
      $$ = ast_make_numbered_line($1, $2);
    }
    | RUN {
      $$ = ast_make_command_run();
    }
    ;

statement:
    PRINT expression {
      $$ = ast_make_print($2);
    }
    | IF expression THEN statement {
      $$ = ast_make_if($2, $4);
    }
    ;

expression:
    IDENTIFIER {
      $$ = ast_make_id($1);
    }
    | STRING {
      $$ = ast_make_string($1);
    }
    | NUMBER {
      $$ = ast_make_number($1);
    }
    /* | expression '+' expression */
    /* | expression '-' expression */
    /* | expression '*' expression */
    /* | expression '/' expression */
    /* | expression LT expression */
    /* | expression LTE expression */
    /* | expression GT expression */
    /* | expression GTE expression */
    ;
%%