Skip to content

Commit

Permalink
Finally ready v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Centrix14 committed Dec 31, 2019
1 parent f74bc57 commit e18daf0
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
2 changes: 2 additions & 0 deletions arr_lib.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Saol interpreter library v1.0 31/12/2019 by Centrix */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down
1 change: 1 addition & 0 deletions arr_lib.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Header file for arr_lib.c */
#ifndef __SAOL_ARRAY_LIB_H_INCLUDED__
#define __SAOL_ARRAY_LIB_H_INCLUDED__

Expand Down
7 changes: 7 additions & 0 deletions isi.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Saol interactive interpreter file
* v1.0
* 31/12/2019
* by Centrix
*/

#include <stdio.h>
#include <string.h>
#include "unitok/unitok.h"
Expand Down
7 changes: 7 additions & 0 deletions saol.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* SAOL interpreter file
* v1.0
* 31/12/2019
* by Centrix
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
Expand Down
116 changes: 116 additions & 0 deletions ssp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <stdio.h>
#include <string.h>
#include "unitok/unitok.h"

/*
* ssp - standard saol preprocessor
* v1.0
* 31/12/2019
* by Centrix
*/

int is_kw(char* word);
void exec(int op_code, char* arg);
void macro(char *arg), link(char *arg), prog(char *arg), copy(char *arg);

char macro_names[1024][256], macro_codes[1024][256];
int len = 3, code = 3, pos = 0;
FILE *src, *dst;

int main(int argc, char *argv[]) {
char line[1024], *tok = "";

if (argc < 2) {
fprintf(stderr, "Usage: in_file out_file\n");
return 1;
}
src = fopen(argv[1], "r");
dst = fopen(argv[2], "w");

while (fgets(line, 1024, src) != NULL) {
tok = strtok(line, " \n");

while (tok != NULL) {
if (is_kw(tok) >= 0)
code = is_kw(tok);
if (!isempty(tok))
exec(code, tok);

tok = strtok(NULL, " \n");
}
}

fclose(src);
fclose(dst);
return 0;
}

int is_kw(char *word) {
char *words[] = {".macro", ".link", ".prog"};

for (int i = 0; i < len; i++) {
if (!strcmp(words[i], word)) return i;
}
return -1;
}

void exec(int op_code, char *arg) {
void (*funcs[])(char*) = {macro, link, prog, copy};
(*funcs[op_code])(arg);
}

void macro(char *arg) {
static int state = 0;

if (!state) {
state = 1;
return ;
}
else if (state == 1) {
sprintf(macro_names[pos], "%s", arg);
state = 2;
}
else {
sprintf(macro_codes[pos++], "%s", arg);
state = 1;
}
}

void link(char *arg) {
FILE *from;
int c = 0;

if (!strcmp(arg, ".link")) return ;

from = fopen(arg, "r");
if (from == NULL) {
fprintf(stderr, "no such file `%s`\n", arg);
return ;
}

while (c != EOF) {
if (c > 0)
putc(c, dst);
c = fgetc(from);
}
putc('\n', dst);

fclose(from);
}

void prog(char *arg) {
code = 3;
}

int is_macro(char *word) {
for (int i = 0; i < pos; i++)
if (!strcmp(macro_names[i], word)) return i;
return -1;
}

void copy(char *arg) {
if (is_macro(arg) >= 0)
fprintf(dst, "%s", macro_codes[is_macro(arg)]);
else
fprintf(dst, "%s ", arg);
}

0 comments on commit e18daf0

Please sign in to comment.