arisuchan    [ tech / cult / art ]   [ λ / Δ ]   [ psy ]   [ ru ]   [ random ]   [ meta ]   [ all ]    info / stickers     temporarily disabledtemporarily disabled

/λ/ - programming

structure and interpretation of computer programs.
Name
Email
Subject
Comment

formatting options

File
Password (For file deletion.)

Help me fix this shit. https://legacy.arisuchan.jp/q/res/2703.html#2703

Kalyx ######


File: 1492427512219.jpg (54.95 KB, 400x400, UN-at-sign.jpg)

 No.1

I'm currently working on like a silly digital assistant that i started on a long time ago on my C64.
But right now I have it where it can take just some simple single word commands, but I really want to try and get it to break down some sentences and be able to have a very basic conversation and take command stings.

So lets have a parsing engine thread! Text adventures welcome.

Here is what I have so far:

  #include <iostream>
  #include <string>
  #include <fstream>
  #include <cstdlib>
  #include <stdlib.h>
  #include <time.h>
  #include <unistd.h>
  using namespace std;

/*||||||Functions Prototype Start||||||*/

/*||||||Functions Prototype End||||||*/

  int main()
{

/*||||||Variables Start||||||*/
      string mainContVar;
    int menu_return;
/*||||||Variables End||||||*/
    

/*||||||Main Control Loop Start||||||*/
      while(mainContVar != "Close" || mainContVar != "close") {
      cout << "H   H    OOO   PPP   EEE\n";
      cout << "H   H   O   O  P  P  E  \n";
      cout << "HHHHH   O   O  P P   EE \n"; 
      cout << "H   H   O   O  P     E  \n";
      cout << "H   H *  OOO * P  *  EEE *\n\n" <<endl;

      cout << "Hello World! I am H.O.P.E.\n";
      cout << "What can I do for you? ";
      cin >> mainContVar;

          if (mainContVar == "about" || mainContVar == "About")
          {
              cout << endl << "Well I'm H.O.P.E. I'm a digital assitant. Anymore than that and R0gU3 might be mad at me.\n" << endl;
          sleep(5);
          }
        else if (mainContVar == "hello" || mainContVar == "Hello")
        {
          cout << endl << "I already said hello...I mean sure it was to the whole world. But come on, what else you got?\n" << endl;
          sleep(5);
        }
        else if (mainContVar == "poop" || mainContVar == "Poop")
        {
          cout << endl << "Really man. Like is there really a point to that?\n" << endl;
          sleep(5);
        }
         system("cls");
       system("clear");   
      }

/*|||||Main Control Loop End||||||*/
      return(0);

}

 No.2

>>1
Oh, I forgot to mention I'm currently converting it from BASIC to C++.

 No.3

>>2
I'd read it, but the syntax highlighting looks all kinds of fucked up.

One thing I did notice is you're using "system()" quite a bit. This is horrible practice and a portability nightmare.

 No.11

This is some code from a project I started a few days ago. Basically, I call cmdParse() with the input I get from gnu's readline(), then cmdParse() calls cmdProcess() to sanitize input, break the buffer I get from readline() into a series of space separated words then pack those words into a Cmd struct. Then, back in cmdParse() I use the first and second letters of the first word to determine which command the user has typed. I then pass the entire Cmd struct to the function specified in the cmd_table which allows each individual command to access to any potential arguments the user passed.

I removed most of the error checking for readability sake.

For any interested, I'm working on a text based adventure game.

Cmd *
cmdProcess(char *cmd_buffer) {

    char *c = cmd_buffer;
    int word_count, len, pos;
    Cmd *new_cmd;
    new_cmd = malloc(sizeof(Cmd));

    for (word_count = 0, len = 0, pos = 0; word_count < 4; c++, len++) {
        if (*c == ' ' || *c == '\0') {
            char *new_ptr;
            new_ptr = malloc((sizeof(char) * len)+1);    
            if (!new_ptr) die("cmdProcess: for:");
            
            strncpy(new_ptr, cmd_buffer+pos, len);
            new_ptr[len] = '\0';

            new_cmd->words[word_count] = new_ptr;
            word_count++;
            pos = len+1;
            len = 0;
        } else if (*c > 64 && *c < 91) {*c += 32;} // translate upper case
        else if (!(*c > 96 && *c < 123)) {*c = '?';} // translate all else to ?
    }
    
    new_cmd->count = word_count;    
    free(cmd_buffer);
    return new_cmd;
}

int
cmdParse(char *cmd_buffer) {
    
    Cmd *cmd = cmdProcess(cmd_buffer);
    int k = cmd->words[0][0]-97;
    int ey = cmd->words[0][1]-97;

    if ((k < 27 && ey >= 0) && (k< 27 && ey >= 0) && cmd_table[k][ey]) 
        cmd_table[k][ey](cmd);
    else printf("Invalid Command: %s\n", cmd->words[0]);

    for (int i = 0; i < cmd->count; i++)
        free(cmd->words[i]);
    free(cmd);

    return 0;
}


Also, I agree with >>3 you really really shouldn't use system(). If you absolutely must clear the screen use a library like ncurses.

 No.12

>>11
I know this is off-topic, but that is sweet syntax highlighting. I haven't seen it like that on other boards.

 No.13

>>3
>>11

Okay, I change it out, that was just the first solution I found on a fast google search and I never really looked into anything else.


But I like your code 11, I might see if I can mod it for my program.

>>12
I feel like its a valid point, it does look really cool, but 3 is right, the highlighting is off.. So it might be that it isn't picking up on the language correctly.

 No.24

>>1

I was working on a simple parser example a while ago that's quite inefficient but might be something to work from.

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

enum EMaybe{
    Nothing,
    Just,
};

struct Maybe{
    enum EMaybe maybe;
    union{
        int *ip; int i;
        float *fp; float f;
        char *cp; char c; bool b;
    }result;
    size_t length;
};

struct Maybe character(char wanted, char c){
    struct Maybe val = {Nothing, 0};
    if (c == wanted){
        val.maybe = Just;
        val.length = 1;
        val.result.c = c;
    }
    return val;
}

struct Maybe many(struct Maybe (fn)(char), char *text){
    struct Maybe val = {Nothing, 0, 0};
    size_t index;
    for (index = 0; (fn(text[index])).maybe == Just; index++);
    val.maybe = Just;
    val.length = index;
    val.result.cp = text;
    return val;
}

struct Maybe upper(char l){
    struct Maybe val = {Nothing, 0};
    if (l > 64 && l < 91){
        val.maybe = Just;
        val.length = 1;
        val.result.c = l;
    }
    return val;
}

struct Maybe lower(char l){
    struct Maybe val = {Nothing, 0};
    if (l > 96 && l < 124){
        val.maybe = Just;
        val.length = 1;
        val.result.c = l;
    }
    return val;
}

struct Maybe letter(char l){
    struct Maybe val;
    val = upper(l);
    if (val.maybe == Just) return val;
    val = lower(l);
    if (val.maybe == Just) return val;
    val.maybe  = Nothing;
    val.length = 1;
    val.result.c = 0;
    return val;
}

struct Maybe digit(char d){
    struct Maybe val = {Nothing, 0};
    if (d > 47 && d < 58){
        val.maybe = Just;
        val.length = 1;
        val.result.c = d;
    }
    return val;
}

struct Maybe oneOf(char *string, size_t length, char c){
    struct Maybe val = {Nothing, 0};
    size_t index;
    for (index = 0; index < length; index++){
        if (string[index] == c){
            val.maybe = Just;
            val.length = 1;
            val.result.c = c;
            return val;
        }
    }
    return val;
}

#define letters(x) many(letter, x)
#define digits(x) many(digit, x)
#define spaces(x) many(space, x)
#define whitespace(x) oneOf(" \t\n", 3, x)
#define space(x) character(' ', x)
#define eof(x) character('\0', x)
#define newline(x) character('\n', x)
#define tab(x) character('\t', x)
#define symbol(y,x) character(y, x)
#define advance(z,y,x) y = x;z += y.length;\
    if (y.maybe == Nothing){\
        return y;\
    }


It's bad C but ok for safe parsing of data with possible backtracking since it counts the number of characters read.
Thing works by

 No.25

>>17
>I don't think the [.code] tags are cancelling the other formatting symbols.
This has been fixed. You may want to update your posts.

 No.34

File: 1493133588363.gif (266.79 KB, 400x400, have_a_nice_day.gif)

>>25
Thanks



[Return] [Go to top] [ Catalog ] [Post a Reply]
Delete Post [ ]