Added I/O operations (IN and OUT commands)
This commit is contained in:
parent
d7c038066c
commit
12f904ef7a
@ -7,6 +7,7 @@ Stack based virtual machine and assembly code compiler
|
|||||||
- Assembly
|
- Assembly
|
||||||
- Stack operations (push & pop)
|
- Stack operations (push & pop)
|
||||||
- Arithmetics operations
|
- Arithmetics operations
|
||||||
|
- I/O operations
|
||||||
|
|
||||||
## Assembly documentation
|
## Assembly documentation
|
||||||
|
|
||||||
@ -28,6 +29,10 @@ Stack based virtual machine and assembly code compiler
|
|||||||
Removes two top values in stack and pushes integer part of division
|
Removes two top values in stack and pushes integer part of division
|
||||||
- **MOD**
|
- **MOD**
|
||||||
Removes two top values in stack and pushes modulo
|
Removes two top values in stack and pushes modulo
|
||||||
|
- **IN**
|
||||||
|
Inputs string with C I/O operators
|
||||||
|
- **OUT**
|
||||||
|
Outputs string with C I/O operators
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
|
@ -8,4 +8,8 @@ cmd_desc_t cmd_desc[] = {
|
|||||||
{MUL, 0, "MUL"},
|
{MUL, 0, "MUL"},
|
||||||
{DIV, 0, "DIV"},
|
{DIV, 0, "DIV"},
|
||||||
{MOD, 0, "MOD"},
|
{MOD, 0, "MOD"},
|
||||||
|
#ifdef IO_OPERATIONS
|
||||||
|
{IN, 0, "IN"},
|
||||||
|
{OUT, 0, "OUT"},
|
||||||
|
#endif
|
||||||
{NONE, 0, "NONE"}}
|
{NONE, 0, "NONE"}}
|
||||||
|
@ -10,6 +10,10 @@ enum command_e
|
|||||||
MUL,
|
MUL,
|
||||||
DIV,
|
DIV,
|
||||||
MOD,
|
MOD,
|
||||||
|
#ifdef IO_OPERATIONS
|
||||||
|
IN,
|
||||||
|
OUT,
|
||||||
|
#endif
|
||||||
NONE
|
NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
39
run.c
39
run.c
@ -123,8 +123,45 @@ int exec(command_t cmd, stack_t *stack)
|
|||||||
return *res;
|
return *res;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
#ifdef IO_OPERATIONS
|
||||||
|
/**
|
||||||
|
* Inputs string with C I/O operators
|
||||||
|
*/
|
||||||
|
case IN:
|
||||||
|
{
|
||||||
|
char input[512];
|
||||||
|
scanf("%s", &input);
|
||||||
|
int size = str_len(input);
|
||||||
|
|
||||||
|
for (int pos = size; size >= 0; size--)
|
||||||
|
{
|
||||||
|
stack_push(stack, input[pos], res);
|
||||||
|
if (res != NULL)
|
||||||
|
return *res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
/**
|
||||||
|
* Outputs string with C I/O operators
|
||||||
|
*/
|
||||||
|
case OUT:
|
||||||
|
{
|
||||||
|
char ch = stack_pop(stack, res);
|
||||||
|
if (res != NULL)
|
||||||
|
return *res;
|
||||||
|
|
||||||
|
while (ch != NULL)
|
||||||
|
{
|
||||||
|
putchar(ch);
|
||||||
|
ch = stack_pop(stack, res);
|
||||||
|
if (res != NULL)
|
||||||
|
return *res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
default:
|
default:
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
5
run.h
5
run.h
@ -3,6 +3,11 @@
|
|||||||
|
|
||||||
#include "./stack.h"
|
#include "./stack.h"
|
||||||
#include "./command.h"
|
#include "./command.h"
|
||||||
|
#include "./utils.h"
|
||||||
|
|
||||||
|
#ifdef IO_OPERATIONS
|
||||||
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
int run(command_t **buff, stack_t *stack);
|
int run(command_t **buff, stack_t *stack);
|
||||||
int exec(command_t cmd, stack_t *stack);
|
int exec(command_t cmd, stack_t *stack);
|
||||||
|
6
stack.c
6
stack.c
@ -30,12 +30,12 @@ int stack_push(stack_t *stack, int value, int *result)
|
|||||||
|
|
||||||
if (stack->cursor > stack->size - 1)
|
if (stack->cursor > stack->size - 1)
|
||||||
{
|
{
|
||||||
*result = 1;
|
*result = 2;
|
||||||
return *result;
|
return *result;
|
||||||
}
|
}
|
||||||
|
|
||||||
stack->mem[stack->cursor + 1] = value;
|
stack->mem[stack->cursor + 1] = value;
|
||||||
stack->cursor += 1;
|
stack->cursor++;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ int stack_pop(stack_t *stack, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int value = stack->mem[stack->cursor];
|
int value = stack->mem[stack->cursor];
|
||||||
stack->cursor -= 1;
|
stack->cursor--;
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
11
utils.c
Normal file
11
utils.c
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "./utils.h"
|
||||||
|
|
||||||
|
int str_len(char str[])
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
|
||||||
|
while (str[pos] != NULL)
|
||||||
|
pos++;
|
||||||
|
|
||||||
|
return pos;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user