go-analyzer/analyzers/test/test.y

176 lines
4.0 KiB
Plaintext

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
extern int yylineno;
extern char *yytext;
extern void yyerror(const char *s);
extern int yylex();
extern FILE *yyin;
void free_node(char *str) {
if (str) free(str);
}
%}
%union {
char *str;
double num;
}
%token SHORT_DECLARATION LBRACE RBRACE SEMICOLON ASSIGN LPAREN RPAREN COMMA
%token VAR FUNC RETURN STRING_LITERAL FLOAT_LITERAL NUMBER PACKAGE IMPORT
%token PLUS MINUS MULT DIV MOD
%token STRING
%token UINT UINT8 UINT16 UINT32 UINT64
%token INT INT8 INT16 INT32 INT64
%token <str> IDENTIFIER
%left PLUS MINUS
%left MULT DIV MOD
%left UMINUS
%%
program:
package_declaration import_declaration
| program statement
;
statement:
| var_declaration SEMICOLON
| func_declaration
;
block:
LBRACE statements_list RBRACE
;
statements_list:
| statements_list statement
| statements_list block
| statements_list expr SEMICOLON
;
expr:
RETURN math_expr { printf("\033[1;35mRETURN math expr\033[0m\n") }
| RETURN literal { printf("\033[1;35mRETURN literal\033[0m\n") }
| IDENTIFIER ASSIGN math_expr { }
| math_expr { }
;
math_expr:
math_expr PLUS math_expr { printf("PLUS\n"); }
| math_expr MINUS math_expr { printf("MINUS\n"); }
| math_expr MULT math_expr { printf("MULT\n"); }
| math_expr DIV math_expr { printf("DIV\n"); }
| math_expr MOD math_expr { printf("MOD\n"); }
| MINUS math_expr %prec UMINUS { printf("UMINUS\n"); }
| LPAREN { printf("LPAREN\n"); } math_expr RPAREN { printf("RPAREN\n"); }
| NUMBER { printf("NUMBER\n"); }
| FLOAT_LITERAL { printf("FLOAT LITERAL\n"); }
| IDENTIFIER { printf("IDENTIFIER: %s\n", $1); }
;
int_types:
UINT { }
| UINT8 { }
| UINT16 { }
| UINT32 { }
| UINT64 { }
| INT { }
| INT8 { }
| INT16 { }
| INT32 {}
| INT64 { }
;
string_types:
STRING { }
;
type:
int_types { }
| string_types { }
;
literal:
STRING_LITERAL { }
| FLOAT_LITERAL { }
| NUMBER { }
;
// Package & import blocks
package_declaration:
PACKAGE IDENTIFIER SEMICOLON
{ printf("\033[1;34mPACKAGE IDENTIFIER: %s\n\033[0m", $2); }
;
import_declaration:
| IMPORT { printf("\033[1;36mHELLO, IMPORT BLOCK\n\033[0m"); } import { printf("\033[1;36mBY, IMPORT BLOCK\n\n\033[0m"); }
| IMPORT { printf("\033[1;36mHELLO, IMPORT BLOCK\n\033[0m"); } LPAREN import_list RPAREN { printf("\033[1;36mBY, IMPORT BLOCK\n\n\033[0m"); }
;
import:
IDENTIFIER STRING_LITERAL { printf("\033[1;36mIMPORTED PACKAGE\n\033[0m"); } SEMICOLON
| STRING_LITERAL { printf("\033[1;36mIMPORTED PACKAGE\n\033[0m"); } SEMICOLON
;
import_list:
import
| import_list import
;
//
// functions decl
arg_declaration:
IDENTIFIER type
{ printf("\033[1;35mARG: %s\n\033[0m", $1); }
;
arg_list:
| arg_declaration
| arg_list COMMA arg_declaration
;
return_type:
| type { }
;
func_declaration:
FUNC IDENTIFIER
{ printf("\033[1;35mHELLO, FUNC: %s\n\033[0m", $2); }
LPAREN arg_list RPAREN return_type block
{ printf("\033[1;35mBY, FUNC: %s\n\n\033[0m", $2); }
;
//
// vars decl
var_declaration:
IDENTIFIER SHORT_DECLARATION math_expr { printf("\033[1;33mSHORT DECL with math expr: %s\n\033[0m", $1); }
| IDENTIFIER SHORT_DECLARATION literal { printf("\033[1;33mSHORT DECL with literal: %s\n\033[0m", $1); }
| VAR IDENTIFIER type { { printf("\033[1;33mVAR DECL without init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER type ASSIGN math_expr { { printf("\033[1;33mVAR DECL with math expr init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER type ASSIGN literal { { printf("\033[1;33mVAR DECL with literal init value: %s\n\033[0m", $2); } }
;
%%
//
int main(int argc, char **argv) {
if (argc > 1) {
FILE *f = fopen(argv[1], "r");
if (!f) {
perror("\033[1;91mFailed to open file\033[0m");
return 1;
}
yyin = f;
}
yyparse();
printf("\033[1;92mGOOD CODE\033[0m\n");
return 0;
}