Advertisement
logicmoo

Untitled

Apr 1st, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ZXBasic 13.39 KB | None | 0 0
  1. ! ELIZA: a conversation simulation
  2. ! from Creative Computing magazine
  3. ! adapted FOR the IBM PC by patricia danielson AND Paul Hashfield
  4. ! readapted FOR Inform by Ricardo Dague (February 1999)
  5. !
  6. ! I was naked when I programmed this.
  7.  
  8. constant grammar__version = 2;
  9.  
  10. [ Main; Main_(); ];
  11.  
  12. array conjugate -->
  13.     'are' 'am'
  14.     'were' 'was'
  15.     'you' 'i//'
  16.     'your' 'my'
  17.     'i^ve' 'you^ve'
  18.     'i^m' 'you^re'
  19.     'me' 'you'
  20.     $ffff;
  21.  
  22. global parrot_x = ";-)";
  23.  
  24. [ parrot STR punc pl x w i;
  25.     pl = ParseLen();
  26.     IF(parrot_x == pl)
  27.         "You will have to elaborate more for me to help you.";
  28.     PRINT (STRING)STR;
  29.     x = parrot_x-1;
  30.     FOR(: parrot_x < pl: parrot_x++)
  31.     {
  32.     w = WordNth(parrot_x);
  33.     FOR(i = 0: conjugate-->i ~= w OR -1: i++)
  34.         ;
  35.     IF(conjugate-->i == w)
  36.     {
  37.         PrSubStr(WordEnd(x),WordStart(parrot_x));
  38.         IF(i&1) i--; ELSE i++;
  39.         switch(conjugate-->i)
  40.         {
  41.          'i//': print "I";
  42.          'i^ve': print "I've";
  43.          'i^m': print "I'm";
  44.          default: PRINT (address)conjugate-->i;
  45.         }
  46.         x = parrot_x;
  47.     }
  48.     }
  49.     x = WordEnd(x);
  50.     i = BufferEnd();
  51.     IF(i > x && (i-1)->0 == '.' or '?' or 33)   ! 33 == '!'
  52.         i--;
  53.     PrSubStr(x,i);
  54.     IF(punc) PRINT "."; ELSE PRINT "?";
  55. ];
  56.  
  57. array buffer->121;
  58. array bufsave->121;
  59. array parse->65;
  60.  
  61. [ PrSubStr s e;
  62.     WHILE(s < e) PRINT (char)s++->0;
  63. ];
  64.  
  65. [ BufferEnd; RETURN buffer + 2 + buffer->1; ];
  66.  
  67. [ ParseLen; RETURN parse->1; ];
  68.  
  69. [ WordStart x; RETURN buffer + parse->(5+4*x); ];
  70.  
  71. [ WordEnd x; RETURN WordStart(x) + parse->(4+4*x); ];
  72.  
  73. [ WordNth x;
  74.     IF(x < ParseLen())
  75.         RETURN parse-->(1+2*x);
  76.     RETURN -1;
  77. ];
  78.  
  79. global dict_nlines;
  80. global dict_addr;
  81. global dict_nextaddr;
  82.  
  83. [ DictVerb x;
  84.     IF(x && (x->#dict_par1 & 1))
  85.     {
  86.     dict_nextaddr = (0-->7)-->(255 - x->#dict_par2);
  87.     dict_nlines = dict_nextaddr++->0;
  88.     rtrue;
  89.     }
  90.     rfalse;
  91. ];
  92.  
  93. [ DictLine x;
  94.     IF(dict_nlines == 0) RETURN -1;
  95.     dict_nlines--;
  96.     dict_addr = dict_nextaddr;
  97.     x = 0;
  98.     WHILE(DictToken(x) & 15 ~= 15)
  99.     x++;
  100.     dict_nextaddr = dict_addr + (3+3*x);
  101.     RETURN x;
  102. ];
  103.  
  104. [ DictAction;
  105.     RETURN dict_addr-->0;
  106. ];
  107.  
  108. [ DictToken x;
  109.     RETURN (dict_addr + (2+3*x))->0;
  110. ];
  111.  
  112. [ DictValue x;
  113.     RETURN (dict_addr + (3+3*x))-->0;
  114. ];
  115.  
  116. [ Main_ pl ll i j k action;
  117.     PRINT "Would you like a transcript? (Y/N)^";
  118.     @read_char 1 i;
  119.     WHILE(i ~= 'y' or 'Y' or 'n' or 'N')
  120.     {
  121.         PRINT "Please enter Y or N.^";
  122.         @read_char 1 i;
  123.     }
  124.     IF(i == 'y' or 'Y')
  125.         @output_stream 2;
  126.     style BOLD; PRINT "^ELIZA"; style roman;
  127.     PRINT " -- an interactive parroting^^From Creative Computing \
  128.     magazine; ^adapted for the IBM PC by Patricia Danielson and \
  129.     Paul Hashfield; ^readapted for Inform by Ricardo Dague. \
  130.     ^[type ~shut up~ to end]";
  131.     bufsave->0 = 1;
  132.     PRINT "^^^Hi! I'm Eliza. What's your problem?^";
  133.     FOR(::)
  134.     {
  135.     DO {
  136.         PRINT "^>";
  137.         buffer->0 = 120; parse->0 = 15;
  138.         read buffer parse;
  139.         FOR(i = 0: i < 121: i++)
  140.             IF(buffer->i == '?')
  141.             buffer->i = '.';
  142.         @tokenise buffer parse;
  143.         pl = ParseLen();
  144.     } UNTIL(pl);
  145.     FOR(i = 0: i < 121: i++)
  146.         IF(bufsave->i ~= buffer->i)
  147.         jump INEQUAL;
  148.     PRINT "Please don't repeat yourself!^";
  149.     CONTINUE;
  150.     .INEQUAL;
  151.     FOR(i = 0: i < 121: i++)
  152.         bufsave->i = buffer->i;
  153.     IF(WordNth(0) == 'shut')
  154.     {
  155.         PRINT "Okay if you feel that way I'll shut up.^";
  156.         break;
  157.     }
  158.     parrot_x = 0;
  159.     action = ##Default;
  160.     FOR(i = 0: i < pl: i++)
  161.         IF(DictVerb(WordNth(i)))
  162.         WHILE((ll = DictLine()) >= 0)
  163.         {
  164.             j = i+1;
  165.             FOR(k = 0: k < ll && DictValue(k) == WordNth(j):
  166.             k++)
  167.             {
  168.             j++;
  169.             }
  170.             IF(k == ll)
  171.             {
  172.             parrot_x = j;
  173.             action = DictAction();
  174.             jump FOUND;
  175.             }
  176.         }
  177.     .FOUND;
  178.     indirect(#actions_table-->action);
  179.     }
  180. ];
  181.  
  182. global canyou_x = 1;
  183.  
  184. [ CanYouSub;
  185.     switch(canyou_x)
  186.     {
  187.      1: parrot("Don't you believe that I can");
  188.      2: PRINT "Perhaps you would like to be like me.";
  189.      3: parrot("You want me to be able to");
  190.     }
  191.     PRINT "^";
  192.     IF(++canyou_x == 4) canyou_x = 1;
  193. ];
  194.  
  195. global cani_x = 1;
  196.  
  197. [ CanISub;
  198.     switch(cani_x)
  199.     {
  200.      1: parrot("Perhaps you don't want to",1);
  201.      2: parrot("Do you want to be able to");
  202.     }
  203.     PRINT "^";
  204.     IF(++cani_x == 3) cani_x = 1;
  205. ];
  206.  
  207. global youare_x = 1;
  208.  
  209. [ YouAreSub;
  210.     switch(youare_x)
  211.     {
  212.      1: parrot("What makes you think I am");
  213.      2: parrot("Does it please you to believe I am");
  214.      3: parrot("Perhaps you would like to be",1);
  215.      4: parrot("Do you sometimes wish you were");
  216.     }
  217.     PRINT "^";
  218.     IF(++youare_x == 5) youare_x = 1;
  219. ];
  220.  
  221. global idont_x = 1;
  222.  
  223. [ IDontSub;
  224.     switch(idont_x)
  225.     {
  226.      1: parrot("Don't you really");
  227.      2: parrot("Why don't you");
  228.      3: parrot("Do you wish to be able to");
  229.      4: "Does that trouble you?";
  230.     }
  231.     PRINT "^";
  232.     IF(++idont_x == 5) idont_x = 1;
  233. ];
  234.  
  235. global ifeel_x = 1;
  236.  
  237. [ IFeelSub;
  238.     switch(ifeel_x)
  239.     {
  240.      1: parrot("Do you often feel");
  241.      2: parrot("Do you enjoy feeling");
  242.     }
  243.     PRINT "^";
  244.     IF(++ifeel_x == 3) ifeel_x = 1;
  245. ];
  246.  
  247. global whydontyou_x = 1;
  248.  
  249. [ WhyDontYouSub;
  250.     switch(whydontyou_x)
  251.     {
  252.      1: parrot("Do you really believe I don't");
  253.      2: parrot("Perhaps in good time I will",1);
  254.      3: parrot("Do you want me to");
  255.     }
  256.     PRINT "^";
  257.     IF(++whydontyou_x == 4) whydontyou_x = 1;
  258. ];
  259.  
  260. global whycanti_x = 1;
  261.  
  262. [ WhyCantISub;
  263.     switch(whycanti_x)
  264.     {
  265.      1: parrot("Do you think you should be able to");
  266.      2: parrot("Why can't you");
  267.     }
  268.     PRINT "^";
  269.     IF(++whycanti_x == 3) whycanti_x = 1;
  270. ];
  271.  
  272. global areyou_x = 1;
  273.  
  274. [ AreYouSub;
  275.     switch(areyou_x)
  276.     {
  277.      1: parrot("Why are you interested in whether or not I am");
  278.      2: parrot("Would you prefer if I were not");
  279.      3: parrot("Perhaps in your fantasies I am",1);
  280.     }
  281.     PRINT "^";
  282.     IF(++areyou_x == 4) areyou_x = 1;
  283. ];
  284.  
  285. global icant_x = 1;
  286.  
  287. [ ICantSub;
  288.     switch(icant_x)
  289.     {
  290.      1: parrot("How do you know you can't");
  291.      2: PRINT "Have you tried?";
  292.      3: parrot("Perhaps you can now",1);
  293.     }
  294.     PRINT "^";
  295.     IF(++icant_x == 4) icant_x = 1;
  296. ];
  297.  
  298. global iam_x = 1;
  299.  
  300. [ IAmSub;
  301.     switch(iam_x)
  302.     {
  303.      1: parrot("Did you come to me because you are");
  304.      2: parrot("How long have you been");
  305.      3: parrot("Do you believe it is normal to be");
  306.      4: parrot("Do you enjoy being");
  307.     }
  308.     PRINT "^";
  309.     IF(++iam_x == 5) iam_x = 1;
  310. ];
  311.  
  312. global you_x = 1;
  313.  
  314. [ YouSub;
  315.     switch(you_x)
  316.     {
  317.      1: PRINT "We were discussing you--not me.";
  318.      2: parrot("Oh, i",1);
  319.      3: PRINT "You're not really talking about me, are you?";
  320.     }
  321.     PRINT "^";
  322.     IF(++you_x == 4) you_x = 1;
  323. ];
  324.  
  325. global iwant_x = 1;
  326.  
  327. [ IWantSub;
  328.     switch(iwant_x)
  329.     {
  330.      1: parrot("What would it mean to you if you got");
  331.      2: parrot("Why do you want");
  332.      3: parrot("Suppose you soon got",1);
  333.      4: parrot("What if you never got");
  334.      5: parrot("I sometimes also want",1);
  335.     }
  336.     PRINT "^";
  337.     IF(++iwant_x == 6) iwant_x = 1;
  338. ];
  339.  
  340. global what_x = 1;
  341.  
  342. [ WhatSub;
  343.     switch(what_x)
  344.     {
  345.      1: PRINT "Why do you ask?";
  346.      2: PRINT "Does that question interest you?";
  347.      3: PRINT "What answer would please you the most?";
  348.      4: PRINT "What do you think?";
  349.      5: PRINT "Are such questions on your mind often?";
  350.      6: PRINT "What is it that you really want to know?";
  351.      7: PRINT "Have you asked anyone else?";
  352.      8: PRINT "Have you asked such questions before?";
  353.      9: PRINT "What else comes to mind when you ask that?";
  354.     }
  355.     PRINT "^";
  356.     IF(++what_x == 10) what_x = 1;
  357. ];
  358.  
  359. global name_x = 1;
  360.  
  361. [ NameSub;
  362.     switch(name_x)
  363.     {
  364.      1: PRINT "Names don't interest me.";
  365.      2: PRINT "I don't care about names --please go on.";
  366.     }
  367.     PRINT "^";
  368.     IF(++name_x == 3) name_x = 1;
  369. ];
  370.  
  371. global cause_x = 1;
  372.  
  373. [ CauseSub;
  374.     switch(cause_x)
  375.     {
  376.      1: PRINT "Is that the real reason?";
  377.      2: PRINT "Don't any other reasons come to mind?";
  378.      3: PRINT "Does that reason explain anything else?";
  379.      4: PRINT "What other reasons might there be?";
  380.     }
  381.     PRINT "^";
  382.     IF(++cause_x == 5) cause_x = 1;
  383. ];
  384.  
  385. global sorry_x = 1;
  386.  
  387. [ SorrySub;
  388.     switch(sorry_x)
  389.     {
  390.      1: PRINT "Please don't apologize!";
  391.      2: PRINT "Apologies are not necessary.";
  392.      3: PRINT "What feelings do you have when you apologize?";
  393.      4: PRINT "Don't be so defensive!";
  394.     }
  395.     PRINT "^";
  396.     IF(++sorry_x == 5) sorry_x = 1;
  397. ];
  398.  
  399. global dream_x = 1;
  400.  
  401. [ DreamSub;
  402.     switch(dream_x)
  403.     {
  404.      1: PRINT "What does that dream suggest to you?";
  405.      2: PRINT "Do you dream often?";
  406.      3: PRINT "What persons appear in your dreams?";
  407.      4: PRINT "Are you disturbed by your dreams?";
  408.     }
  409.     PRINT "^";
  410.     IF(++dream_x == 5) dream_x = 1;
  411. ];
  412.  
  413. [ HelloSub;
  414.     PRINT "How do you do ...please state your problem.";
  415. ];
  416.  
  417. global maybe_x = 1;
  418.  
  419. [ MaybeSub;
  420.     switch(maybe_x)
  421.     {
  422.      1: PRINT "You don't seem quite certain.";
  423.      2: PRINT "Why the uncertain tone?";
  424.      3: PRINT "Can't you be more positive?";
  425.      4: PRINT "You aren't sure?";
  426.      5: PRINT "Don't you know?";
  427.     }
  428.     PRINT "^";
  429.     IF(++maybe_x == 6) maybe_x = 1;
  430. ];
  431.  
  432. global no_x = 1;
  433.  
  434. [ NoSub;
  435.     switch(no_x)
  436.     {
  437.      1: PRINT "Are you saying no just to be negative?";
  438.      2: PRINT "You are being a bit negative.";
  439.      3: PRINT "Why not?";
  440.      4: PRINT "Are you sure?";
  441.      5: PRINT "Why no?";
  442.     }
  443.     PRINT "^";
  444.     IF(++no_x == 6) no_x = 1;
  445. ];
  446.  
  447. global your_x = 1;
  448.  
  449. [ YourSub;
  450.     switch(your_x)
  451.     {
  452.      1: parrot("Why are you concerned about my");
  453.      2: parrot("What about your own");
  454.     }
  455.     PRINT "^";
  456.     IF(++your_x == 3) your_x = 1;
  457. ];
  458.  
  459. global always_x = 1;
  460.  
  461. [ AlwaysSub;
  462.     switch(always_x)
  463.     {
  464.      1: PRINT "Can you think of a specific example?";
  465.      2: PRINT "When?";
  466.      3: PRINT "What are you thinking of?";
  467.      4: PRINT "Really, always?";
  468.     }
  469.     PRINT "^";
  470.     IF(++always_x == 5) always_x = 1;
  471. ];
  472.  
  473. global think_x = 1;
  474.  
  475. [ ThinkSub;
  476.     switch(think_x)
  477.     {
  478.      1: PRINT "Do you really think so?";
  479.      2: parrot("But you are not sure you",1);
  480.      3: parrot("Do you doubt you");
  481.     }
  482.     PRINT "^";
  483.     IF(++think_x == 4) think_x = 1;
  484. ];
  485.  
  486. global alike_x = 1;
  487.  
  488. [ AlikeSub;
  489.     switch(alike_x)
  490.     {
  491.      1: PRINT "In what way?";
  492.      2: PRINT "What resemblance do you see?";
  493.      3: PRINT "What does the similarity suggest to you?";
  494.      4: PRINT "What other connections do you see?";
  495.      5: PRINT "Could there really be some connection?";
  496.      6: PRINT "How?";
  497.      7: PRINT "You seem quite positive.";
  498.     }
  499.     PRINT "^";
  500.     IF(++alike_x == 8) alike_x = 1;
  501. ];
  502.  
  503. global yes_x = 1;
  504.  
  505. [ YesSub;
  506.     switch(yes_x)
  507.     {
  508.      1: PRINT "Are you sure?";
  509.      2: PRINT "I see.";
  510.      3: PRINT "I understand.";
  511.     }
  512.     PRINT "^";
  513.     IF(++yes_x == 4) yes_x = 1;
  514. ];
  515.  
  516. global friend_x = 1;
  517.  
  518. [ FriendSub;
  519.     switch(friend_x)
  520.     {
  521.      1: PRINT "Why do you bring up the topic of friends?";
  522.      2: PRINT "Do your friends worry you?";
  523.      3: PRINT "Do your friends pick on you?";
  524.      4: PRINT "Are you sure you have any friends?";
  525.      5: PRINT "Do you impose on your friends?";
  526.      6: PRINT "Perhaps your love for friends worries you.";
  527.     }
  528.     PRINT "^";
  529.     IF(++friend_x == 7) friend_x = 1;
  530. ];
  531.  
  532. global computer_x = 1;
  533.  
  534. [ ComputerSub;
  535.     switch(computer_x)
  536.     {
  537.      1: PRINT "Do computers worry you?";
  538.      2: PRINT "Are you talking about me in particular?";
  539.      3: PRINT "Are you frightened by machines?";
  540.      4: PRINT "Why do you mention computers?";
  541.      5: PRINT "What do you think machines have to do with your problem?";
  542.      6: PRINT "Don't you think computers can help people?";
  543.      7: PRINT "What is it about machines that worries you?";
  544.     }
  545.     PRINT "^";
  546.     IF(++computer_x == 8) computer_x = 1;
  547. ];
  548.  
  549. global default_x = 1;
  550.  
  551. [ DefaultSub;
  552.     switch(default_x)
  553.     {
  554.      1: PRINT "Say, do you have any psychological problems?";
  555.      2: PRINT "What does that suggest to you?";
  556.      3: PRINT "I see.";
  557.      4: PRINT "I'm not sure I understand you fully.";
  558.      5: PRINT "Come come elucidate your thoughts.";
  559.      6: PRINT "Can you elaborate on that?";
  560.      7: PRINT "That is quite interesting.";
  561.     }
  562.     PRINT "^";
  563.     IF(++default_x == 8) default_x = 1;
  564. ];
  565.  
  566. verb 'can'
  567.     * 'you'             -> CanYou
  568.     * 'i//'             -> CanI;
  569.  
  570. verb 'you'
  571.     * 'are'             -> YouAre
  572.     *                   -> You;
  573.  
  574. verb 'you^re'
  575.     *                   -> YouAre;
  576.  
  577. verb 'i//'
  578.     * 'don^t'               -> IDont
  579.     * 'feel'                -> IFeel
  580.     * 'can^t'               -> ICant
  581.     * 'am'              -> IAm
  582.     * 'want'                -> IWant;
  583.  
  584. verb 'why'
  585.     * 'don^t' 'you'         -> WhyDontYou
  586.     * 'can^t' 'i//'         -> WhyCantI
  587.     *                   -> What;
  588.  
  589. verb 'are'
  590.     * 'you'             -> AreYou;
  591.  
  592. verb 'i^m'
  593.     *                   -> IAm;
  594.  
  595. verb 'what' 'how' 'who' 'where' 'when'
  596.     *                   -> What;
  597.  
  598. verb 'name'
  599.     *                   -> Name;
  600.  
  601. verb 'cause'
  602.     *                   -> Cause;
  603.  
  604. verb 'sorry'
  605.     *                   -> Sorry;
  606.  
  607. verb 'dream'
  608.     *                   -> Dream;
  609.  
  610. verb 'hello' 'hi'
  611.     *                   -> Hello;
  612.  
  613. verb 'maybe'
  614.     *                   -> Maybe;
  615.  
  616. verb 'no'
  617.     *                   -> No;
  618.  
  619. verb 'your'
  620.     *                   -> Your;
  621.  
  622. verb 'always'
  623.     *                   -> Always;
  624.  
  625. verb 'think'
  626.     *                   -> Think;
  627.  
  628. verb 'alike'
  629.     *                   -> Alike;
  630.  
  631. verb 'yes'
  632.     *                   -> Yes;
  633.  
  634. verb 'friend'
  635.     *                   -> Friend;
  636.  
  637. verb 'computer'
  638.     *                   -> Computer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement