Skip to content

Commit

Permalink
v1.4: added comparison operations and keyboard input
Browse files Browse the repository at this point in the history
  • Loading branch information
Centrix14 committed Jan 3, 2020
1 parent 34f9762 commit ceb1f63
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
79 changes: 72 additions & 7 deletions arr_lib.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/* Saol interpreter library v1.3 02/01/2020 by Centrix */
/*
* Saol interpreter library
* v1.4
* 02.01.2020
* by Centrix
*/

#include <stdio.h>
#include <stdlib.h>
Expand All @@ -13,9 +18,7 @@ int arr[MAX];
int pos = 0;

int arg_type = 0; // 0 - integer, 1 - character
int start = 0, end = MAX;
int len = 25;
int pred = 1;
int start = 0, end = MAX, len = 32, pred = 1, stop = 0;

void ini(char *arg) {
if (!strcmp(arg, "il")) {
Expand Down Expand Up @@ -168,7 +171,7 @@ void iter(char *arg) {

indx = 0;
tok = get_token(arg, &indx);
while (tok != NULL) {
while (tok != NULL && !stop) {
if (is_kw(arg) >= 0)
code = is_kw(arg);
if (!is_empty(arg))
Expand All @@ -177,6 +180,7 @@ void iter(char *arg) {
tok = get_token(arg, &indx);
}
}
stop = 0;
}

void swap(char *arg) {
Expand All @@ -193,6 +197,67 @@ void swap(char *arg) {
}
}

void inc(char *arg) {
int c = getc(stdin);

if (c == '\n') stop = 1;
arr[pos] = c;
}

void is_bigger(char *arg) {
if (!strcmp(arg, ">?")) return ;

if (isint(arg)) {
if (arr[pos] > atoi(arg)) pred = 1;
else pred = 0;
}
}

void is_smaller(char *arg) {
if (!strcmp(arg, "<?")) return ;

if (isint(arg)) {
if (arr[pos] < atoi(arg)) pred = 1;
else pred = 0;
}
}

void is_bigger_or_eq(char *arg) {
if (!strcmp(arg, ">=?")) return ;

if (isint(arg)) {
if (arr[pos] >= atoi(arg)) pred = 1;
else pred = 0;
}
}

void is_smaller_or_eq(char *arg) {
if (!strcmp(arg, "<=?")) return ;

if (isint(arg)) {
if (arr[pos] <= atoi(arg)) pred = 1;
else pred = 0;
}
}

void is_eq(char *arg) {
if (!strcmp(arg, "=?")) return ;

if (isint(arg)) {
if (arr[pos] == atoi(arg)) pred = 1;
else pred = 0;
}
}

void is_not_eq(char *arg) {
if (!strcmp(arg, "!=?")) return ;

if (isint(arg)) {
if (arr[pos] != atoi(arg)) pred = 1;
else pred = 0;
}
}

int need_exec(int code) {
if ((code > 9 && code < 14) || code > 18) return 1;
else if (pred) return 1;
Expand All @@ -207,7 +272,7 @@ int is_empty(char *word) {
}

int is_kw(char *word) {
char *kws[] = {":", "^", "<", ">", "_!", "~", "|", "-!", ";", "@", "+", "\'", "*", "/", "#", "[", "]", ":-", "!-", "&", "\\", "?~", "?!", "|~", "<->"};
char *kws[] = {":", "^", "<", ">", "_!", "~", "|", "-!", ";", "@", "+", "\'", "*", "/", "#", "[", "]", ":-", "!-", "&", "\\", "?~", "?!", "|~", "<->", "\"", ">?", "<?", ">=?", "<=?", "=?", "!=?"};

for (int i = 0; i < len; i++) {
if (!strcmp(kws[i], word)) return i;
Expand All @@ -216,7 +281,7 @@ int is_kw(char *word) {
}

void exec(int arr_index, char *arg) {
void (*arr[])(char *) = {ini, addr, shl, shr, show_elm, comment, filler, show_arr, int_mode, char_mode, sum, subt, mult, idiv, cla, pstart, pend, assig, notassig, andf, orf, predf, unpred, iter, swap};
void (*arr[])(char *) = {ini, addr, shl, shr, show_elm, comment, filler, show_arr, int_mode, char_mode, sum, subt, mult, idiv, cla, pstart, pend, assig, notassig, andf, orf, predf, unpred, iter, swap, inc, is_bigger, is_smaller, is_bigger_or_eq, is_smaller_or_eq, is_eq, is_not_eq};

if (arr_index >= 0 && arr_index < len && need_exec(arr_index)) (*arr[arr_index])(arg);
}
2 changes: 2 additions & 0 deletions arr_lib.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Header file for arr_lib.c */

#ifndef __SAOL_ARRAY_LIB_H_INCLUDED__
#define __SAOL_ARRAY_LIB_H_INCLUDED__

Expand Down Expand Up @@ -27,6 +28,7 @@ void iter(char *arg);
void predf(char *arg);
void unpred(char *arg);
void swap(char *arg);
void inc(char *arg);

int is_empty(char *word);
int is_kw(char *word);
Expand Down
4 changes: 2 additions & 2 deletions isi.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Saol interactive interpreter file
* v1.0
* 31/12/2019
* 31.12.2019
* by Centrix
*/

Expand All @@ -15,7 +15,7 @@ int main(void) {
char *tok;
int code = -1, pos = 0;

printf("-- Interactive SAOL Interpreter v1.0\n\n");
printf("-- Interactive SAOL Interpreter v1.4\n\n");

while (strcmp(line, "q")) {
printf("\n> ");
Expand Down
2 changes: 1 addition & 1 deletion saol.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SAOL interpreter file
* v1.0
* 31/12/2019
* 31.12.2019
* by Centrix
*/

Expand Down

0 comments on commit ceb1f63

Please sign in to comment.