new generated file + genscript + skeleton of interpreter
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/* Copyright 2014 Samsung Electronics Co., Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "error.h"
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include "pretty-printer.h"
|
||||
#include "interpreter.h"
|
||||
#include "opcode.h"
|
||||
|
||||
|
||||
void
|
||||
safe_opcode (FILE *file, opcode_ptr ptr, int arg1, int arg2)
|
||||
{
|
||||
curr_opcode.func = ptr;
|
||||
curr_opcode.arg1 = arg1;
|
||||
curr_opcode.arg2 = arg2;
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
fwrite (&curr_opcode, sizeof (struct opcode_packed), 1, file);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
gen_bytecode (FILE *src_file)
|
||||
{
|
||||
statement *st;
|
||||
lexer_set_file (src_file);
|
||||
parser_init ();
|
||||
st = parser_parse_statement ();
|
||||
assert (st);
|
||||
while (st->type != STMT_EOF)
|
||||
{
|
||||
pp_statement (st);
|
||||
st = parser_parse_statement ();
|
||||
assert (st);
|
||||
}
|
||||
pp_finish ();
|
||||
|
||||
FILE *file = fopen (FILE_NAME, "w+b");
|
||||
int i;
|
||||
|
||||
for (i = 0; i <= 10; i++)
|
||||
{
|
||||
safe_opcode (file, control_op, i, i);
|
||||
safe_opcode (file, decl_op, i, i);
|
||||
safe_opcode (file, call_op, i, i);
|
||||
}
|
||||
|
||||
fclose (file);
|
||||
}
|
||||
|
||||
void
|
||||
run_int ()
|
||||
{
|
||||
FILE *file = fopen (FILE_NAME, "rb");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
fputs ("File error", stderr);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
while (!feof (file))
|
||||
{
|
||||
fread (&curr_opcode, sizeof (struct opcode_packed), 1, file);
|
||||
|
||||
//printf ("read %d, %d, %p\n", curr_opcode.arg1, curr_opcode.arg2, curr_opcode.func);
|
||||
curr_opcode.func (curr_opcode.arg1, curr_opcode.arg2);
|
||||
}
|
||||
|
||||
fclose (file);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/* Copyright 2014 Samsung Electronics Co., Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: interpreter.h
|
||||
* Author: egavrin
|
||||
*
|
||||
* Created on July 2, 2014, 3:10 PM
|
||||
*/
|
||||
|
||||
#ifndef INTERPRETER_H
|
||||
#define INTERPRETER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "opcode.h"
|
||||
|
||||
#define FILE_NAME "application.bin"
|
||||
|
||||
void safe_opcode(FILE *, opcode_ptr, int, int);
|
||||
void gen_bytecode(FILE*);
|
||||
void run_int();
|
||||
|
||||
#endif /* INTERPRETER_H */
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/* Copyright 2014 Samsung Electronics Co., Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "opcode.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
OP_DEFINITION (control_op)
|
||||
{
|
||||
printf ("control_op %d, %d\n", arg1, arg2);
|
||||
}
|
||||
|
||||
OP_DEFINITION (decl_op)
|
||||
{
|
||||
printf ("decl_op %d, %d\n", arg1, arg2);
|
||||
}
|
||||
|
||||
OP_DEFINITION (call_op)
|
||||
{
|
||||
printf ("call_op %d, %d\n", arg1, arg2);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/* Copyright 2014 Samsung Electronics Co., Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: opcode.h
|
||||
* Author: egavrin
|
||||
*
|
||||
* Created on July 2, 2014, 3:12 PM
|
||||
*/
|
||||
|
||||
#ifndef OPCODE_H
|
||||
#define OPCODE_H
|
||||
|
||||
#define OP_RET_TYPE void
|
||||
#define OP_ARG_TYPE int
|
||||
|
||||
#define OP_ATTR_DECL OP_ARG_TYPE, OP_ARG_TYPE
|
||||
#define OP_ATTR_DEF OP_ARG_TYPE arg1, OP_ARG_TYPE arg2
|
||||
|
||||
#define OP_DECLARATION(opname) OP_RET_TYPE opname (OP_ATTR_DECL)
|
||||
#define OP_DEFINITION(opname) OP_RET_TYPE opname (OP_ATTR_DEF)
|
||||
|
||||
// opcode ptr
|
||||
typedef OP_RET_TYPE (*opcode_ptr)(OP_ATTR_DECL);
|
||||
|
||||
struct
|
||||
__attribute__ ((__packed__))
|
||||
opcode_packed
|
||||
{
|
||||
opcode_ptr func;
|
||||
OP_ARG_TYPE arg1;
|
||||
OP_ARG_TYPE arg2;
|
||||
}
|
||||
curr_opcode;
|
||||
|
||||
#define INIT_OPCODE_PTR(func) opcode_ptr func_ptr = ptr;
|
||||
|
||||
OP_DECLARATION (decl_op);
|
||||
OP_DECLARATION (call_op);
|
||||
OP_DECLARATION (control_op);
|
||||
|
||||
#endif /* OPCODE_H */
|
||||
|
||||
Reference in New Issue
Block a user