summaryrefslogtreecommitdiff
path: root/plugins/govm/gram.y
blob: fe1ccc1ca38e4a2ee8483b30ec35a7f061326145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
%{

#include <stdio.h>
#include <stdlib.h>


#include "build.h"


/* De tok.c... */
extern int yylex(void);
typedef struct yy_buffer_state *YY_BUFFER_STATE;
extern YY_BUFFER_STATE yy_scan_string(const char *);
extern void yy_delete_buffer(YY_BUFFER_STATE);


/* Affiche un message d'erreur concernant l'analyse. */
int yyerror(govm_info *, char *);

/* Procède à l'assemblage d'une ligne de code. */
bool process_govm_code(govm_info *, const char *);

%}

%union {

    char *text;
    short/*uint16_t*/ number;

}


%parse-param { govm_info *info }

%token COMMENT

%token ADD AND CALL DIV DUP EQU GOE GT JMP JZ LB LI LOE LT LW LWS MUL NOP NOR NOT OR POP PUSH ROT ROT3 SALLOC SB SHL SHR SUB SW SWS SYSCALL XOR

%token NUMBER LABEL TEXT
%token REG_A REG_B REG_C REG_D REG_E REG_F

%type <text> LABEL TEXT
%type <number> NUMBER


%%

input:
    /* Vide */
    | input line
    ;

line:
    COMMENT
    | LABEL         { register_govm_label(info, $1); }
    | expression
    ;

expression:
    ADD             { encode_govm_instruction(info, GOP_ADD); }
    | AND           { encode_govm_instruction(info, GOP_AND); }
    | data
    | DIV           { encode_govm_instruction(info, GOP_DIV); }
    | DUP           { encode_govm_instruction(info, GOP_DUP); }
    | EQU           { encode_govm_instruction(info, GOP_EQU); }
    | flow_mod
    | GOE           { encode_govm_instruction(info, GOP_GOE); }
    | GT            { encode_govm_instruction(info, GOP_GT); }
    | LI NUMBER     { encode_govm_instruction(info, GOP_LI); encode_govm_number(info, $2); }
    | LOE           { encode_govm_instruction(info, GOP_LOE); }
    | LT            { encode_govm_instruction(info, GOP_LT); }
    | LWS           { encode_govm_instruction(info, GOP_LWS); }
    | MUL           { encode_govm_instruction(info, GOP_MUL); }
    | NOP           { encode_govm_instruction(info, GOP_NOP); }
    | NOR           { encode_govm_instruction(info, GOP_NOR); }
    | NOT           { encode_govm_instruction(info, GOP_NOT); }
    | OR            { encode_govm_instruction(info, GOP_OR); }
    | POP           { encode_govm_instruction(info, GOP_POP); }
    | pop_reg
    | push_reg
    | ROT           { encode_govm_instruction(info, GOP_ROT); }
    | ROT3          { encode_govm_instruction(info, GOP_ROT3); }
    | SALLOC        { encode_govm_instruction(info, GOP_SALLOC); }
    | SHL           { encode_govm_instruction(info, GOP_SHL); }
    | SHR           { encode_govm_instruction(info, GOP_SHR); }
    | SUB           { encode_govm_instruction(info, GOP_SUB); }
    | SWS           { encode_govm_instruction(info, GOP_SWS); }
    | SYSCALL       { encode_govm_instruction(info, GOP_SYSCALL); }
    | XOR           { encode_govm_instruction(info, GOP_XOR); }
    ;

data:
    LB              { encode_govm_instruction(info, GOP_LB); }
    | LW            { encode_govm_instruction(info, GOP_LW); }
    | SB            { encode_govm_instruction(info, GOP_SB); }
    | SW            { encode_govm_instruction(info, GOP_SW); }
    ;

flow_mod:
    CALL            { encode_govm_instruction(info, GOP_CALL); }
    | JMP           { encode_govm_instruction(info, GOP_JMP); }
    | JZ            { encode_govm_instruction(info, GOP_JZ); }
    | CALL TEXT     { encode_reference_to_govm_label(info, GOP_CALL, $2); }
    | JMP TEXT      { encode_reference_to_govm_label(info, GOP_JMP, $2); }
    | JZ TEXT       { encode_reference_to_govm_label(info, GOP_JZ, $2); }
    ;

pop_reg:
    POP REG_A       { encode_govm_instruction(info, GOP_MOV_A); }
    | POP REG_B     { encode_govm_instruction(info, GOP_MOV_B); }
    | POP REG_C     { encode_govm_instruction(info, GOP_MOV_C); }
    | POP REG_D     { encode_govm_instruction(info, GOP_MOV_D); }
    | POP REG_E     { encode_govm_instruction(info, GOP_MOV_E); }
    | POP REG_F     { encode_govm_instruction(info, GOP_MOV_F); }

push_reg:
    PUSH REG_A      { encode_govm_instruction(info, GOP_A_MOV); }
    | PUSH REG_B    { encode_govm_instruction(info, GOP_B_MOV); }
    | PUSH REG_C    { encode_govm_instruction(info, GOP_C_MOV); }
    | PUSH REG_D    { encode_govm_instruction(info, GOP_D_MOV); }
    | PUSH REG_E    { encode_govm_instruction(info, GOP_E_MOV); }
    | PUSH REG_F    { encode_govm_instruction(info, GOP_F_MOV); }


%%


/******************************************************************************
*                                                                             *
*  Paramètres  : info = ensemble d'informations en place.                     *
*                msg  = texte à afficher.                                     *
*                                                                             *
*  Description : Affiche un message d'erreur concernant l'analyse.            *
*                                                                             *
*  Retour      : Statut de sortie à fournir.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

int yyerror(govm_info *info, char *msg)
{
    printf("PARSING ERROR :: %s\n", msg);

    return -1;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info = ensemble d'informations à venir compléter.            *
*                code = ligne de code à traiter.                              *
*                                                                             *
*  Description : Procède à l'assemblage d'une ligne de code.                  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool process_govm_code(govm_info *info, const char *code)
{
	YY_BUFFER_STATE buffer;                 /* Tampon pour bison           */
	int ret;                                /* Bilan de l'appel            */

	buffer = yy_scan_string(code);
	ret = yyparse(info);
	yy_delete_buffer(buffer);

    return (ret == 0);

}