some really good progress
This commit is contained in:
109
main.c
Normal file
109
main.c
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
char* mstr;
|
||||||
|
} mstring;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
int mint;
|
||||||
|
} mint;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
float myfloat;
|
||||||
|
} mfloat;
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum{
|
||||||
|
TOKEN_PLUS,
|
||||||
|
TOKEN_MINUS,
|
||||||
|
TOKEN_INTEGER,
|
||||||
|
intdef,
|
||||||
|
TOKEN_UNKNOWN,
|
||||||
|
} symbols;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
symbols type;
|
||||||
|
char* text;
|
||||||
|
size_t text_len;
|
||||||
|
} Token;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
char *content;
|
||||||
|
// size_t cursor;
|
||||||
|
// size_t line;
|
||||||
|
} Lexer;
|
||||||
|
|
||||||
|
|
||||||
|
// Lexer
|
||||||
|
void lexer_new(char *content, size_t content_len){
|
||||||
|
|
||||||
|
}
|
||||||
|
// Token
|
||||||
|
void lexer_next(Lexer *mylexer){
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Token read_from_tok(char* text, uint cursor){
|
||||||
|
Token mytoks;
|
||||||
|
static char buf[64];
|
||||||
|
size_t i = 0;
|
||||||
|
if (isdigit(text[cursor])) {
|
||||||
|
// Start of an integer token
|
||||||
|
size_t start = cursor;
|
||||||
|
while (isdigit(text[cursor])) {
|
||||||
|
buf[i++] = text[cursor++];
|
||||||
|
}
|
||||||
|
buf[i] = '\0';
|
||||||
|
mytoks.type = TOKEN_INTEGER;
|
||||||
|
mytoks.text = buf;
|
||||||
|
mytoks.text_len = i;
|
||||||
|
}else {
|
||||||
|
buf[0] = text[cursor];
|
||||||
|
buf[1] = '\0';
|
||||||
|
mytoks.text = buf;
|
||||||
|
|
||||||
|
switch (text[cursor]){
|
||||||
|
case '+':
|
||||||
|
mytoks.type = TOKEN_PLUS;
|
||||||
|
// asigning text is not really needed unless for debug
|
||||||
|
mytoks.text = "+";
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
mytoks.type = TOKEN_MINUS;
|
||||||
|
mytoks.text = "-";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
mytoks.type = TOKEN_UNKNOWN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mytoks;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Token* c
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// operators accepted in int/digit or whatever type def only when they have a digit before AND after them
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
Token newtok;
|
||||||
|
char* input = "32323 + 232";
|
||||||
|
int length1 = strlen(input);
|
||||||
|
int i = 0;
|
||||||
|
while (i < length1) {
|
||||||
|
Token result = read_from_tok(input, i);
|
||||||
|
printf("text: %s\ntype: %u\n\n", result.text, result.type);
|
||||||
|
if (result.type == TOKEN_INTEGER) {
|
||||||
|
i += result.text_len; // to skip the whole integer
|
||||||
|
} else {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
111
nb.h
Normal file
111
nb.h
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
int capacity;
|
||||||
|
int arrsize;
|
||||||
|
char** value;
|
||||||
|
} nb_arr;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
char* nb_strdup(const char* s) {
|
||||||
|
char* d = malloc(strlen(s) + 1);
|
||||||
|
if (d) strcpy(d, s);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
void nb_init(nb_arr *newarr, int initial_capacity){
|
||||||
|
newarr->value = (char**)malloc(sizeof(char*) * initial_capacity);
|
||||||
|
newarr->capacity = initial_capacity;
|
||||||
|
newarr->arrsize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// later increase cap by size of new sheiSSe
|
||||||
|
void nb_append(nb_arr *newarr, char *newval){
|
||||||
|
if (newarr->value == NULL){
|
||||||
|
newarr->capacity =16;
|
||||||
|
if (newarr->capacity > 16 | newarr->arrsize > newarr->capacity) {
|
||||||
|
newarr->capacity *=2;
|
||||||
|
}
|
||||||
|
newarr->value = (char**)realloc(newarr->value, sizeof(char*) * newarr->capacity);
|
||||||
|
}
|
||||||
|
newarr->value[newarr->arrsize++] = strdup(newval);
|
||||||
|
}
|
||||||
|
|
||||||
|
void nb_append_int(nb_arr *newarr, int myint){
|
||||||
|
char buf[64];
|
||||||
|
sprintf(buf, "%d", myint);
|
||||||
|
nb_append(newarr, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void nb_append_float(nb_arr *newarr, float myfloat){
|
||||||
|
char buf[64];
|
||||||
|
sprintf(buf, "%f", myfloat);
|
||||||
|
nb_append(newarr, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void nb_print(nb_arr *newarr){
|
||||||
|
for (int i = 0; i < newarr->arrsize; i++){
|
||||||
|
printf("%s\n", newarr->value[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void nb_print_info(nb_arr *newarr){
|
||||||
|
printf("[INFO] ");
|
||||||
|
for (int i = 0; i < newarr->arrsize; i++){
|
||||||
|
printf("%s", newarr->value[i]);
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void nb_free(nb_arr *newarr){
|
||||||
|
if (newarr->value != NULL){
|
||||||
|
for (int i=0; i < newarr->arrsize; i++){
|
||||||
|
free(newarr->value[i]);
|
||||||
|
newarr->value[i] = NULL;
|
||||||
|
}
|
||||||
|
free(newarr->value);
|
||||||
|
newarr->value = NULL;
|
||||||
|
}
|
||||||
|
newarr -> capacity = 0;
|
||||||
|
newarr -> arrsize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void nb_cmd(nb_arr *newarr){
|
||||||
|
|
||||||
|
if (newarr->arrsize < 1){
|
||||||
|
printf("USAGE: provide more parameters\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
char* cmd = (char*)malloc(sizeof(char*) *newarr->capacity);
|
||||||
|
for (int i=0; i < newarr->arrsize; i++){
|
||||||
|
|
||||||
|
strcat(cmd, strcat(newarr->value[i]," "));
|
||||||
|
}
|
||||||
|
system(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// compile func that requires c_file to run otherwise returns error like <please return usage>
|
||||||
|
void nb_com(nb_arr *newarr){
|
||||||
|
char* cmd = (char*)malloc(sizeof(char*) *newarr->capacity);
|
||||||
|
for (int i=0; i < newarr->arrsize; i++){
|
||||||
|
|
||||||
|
strcat(cmd, strcat(newarr->value[i]," "));
|
||||||
|
}
|
||||||
|
system(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void append_c_file(FILE *filepointer){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
22
nob.c
Normal file
22
nob.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "nb.h"
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
nb_arr cmd;
|
||||||
|
|
||||||
|
nb_append(&cmd, "gcc");
|
||||||
|
nb_append(&cmd, "-Wall -Wextra");
|
||||||
|
nb_append(&cmd, "main.c");
|
||||||
|
nb_append(&cmd, "-o newl");
|
||||||
|
|
||||||
|
nb_print_info(&cmd);
|
||||||
|
|
||||||
|
nb_cmd(&cmd);
|
||||||
|
|
||||||
|
nb_free(&cmd);
|
||||||
|
|
||||||
|
nb_append(&cmd, "./newl");
|
||||||
|
|
||||||
|
nb_print_info(&cmd);
|
||||||
|
|
||||||
|
nb_cmd(&cmd);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user