项目作者: hchiam

项目描述 :
Conversational Code Creator
高级语言: JavaScript
项目地址: git://github.com/hchiam/ccc.git
创建时间: 2018-09-30T14:42:56Z
项目社区:https://github.com/hchiam/ccc

开源协议:MIT License

下载


Conversational Code Creator

Live demo: https://codepen.io/hchiam/full/NLVQeo

Conversationally create code with this interactive programming interface.

You describe a code instruction, and the interface will ask you questions to implement it.

UPDATE: won’t be working on this anymore - see repos like:

Ideas

Maybe this tool could enable even higher-level programming?

Encourage clean code thinking? Think in terms of nouns and verbs to create more declarative code?

Technical Notes

  • This project uses compromise.js to parse the user input sentence for verbs, nouns, and prepositions.
    • Verbs are treated as function names.
    • Nouns are treated as variables or parameter names.
    • Prepositions can be treated as parameter separators (or as a parameter when there are no nouns, e.g. the “up” in “move up”).
  • To make it easier to generate and edit the output code (especially nested objects and functions), I’m trying this out:
    • User input gets turned into functions and variables (treated as objects) stored in one big JS object (instead of relying on lines and line numbers).
    • That JS object is used to generate JavaScript code, to be displayed back to the user.
    • Since JS objects support nesting, there can be nesting of functions and variables (all treated as objects.
    • All variables are initialized as JS objects.

Hello World Example:

User: “say something”

CCC: “What is the initial value of something?”

User: “Hello world!”

This conversation generates this JSON object:

  1. {
  2. "something": "\"Hello world\"",
  3. "say": "function(words) { /*uses external library*/responsiveVoice.speak('\"' + words + '\"', 'UK English Male'); }"
  4. }

Which generates this JS code:

  1. // VARIABLES and FUNCTIONS:
  2. let something = "Hello world";
  3. let say = function (words) {
  4. /*uses external library*/ responsiveVoice.speak(
  5. '"' + words + '"',
  6. "UK English Male"
  7. );
  8. };
  9. // USAGE:
  10. this.resetSharedVariables();
  11. say(something);

Object Creation Example:

User: “fox says words”

CCC: “What is the initial value of words?”

User: “hi”

This conversation generates this JSON object:

  1. {
  2. "fox": {
  3. "say": "function(words) { /*uses external library*/responsiveVoice.speak('\"' + words + '\"', 'UK English Male'); }"
  4. },
  5. "words": "\"hi\""
  6. }

Which generates this JS code:

  1. // VARIABLES and FUNCTIONS:
  2. let fox = {};
  3. fox.say = function (words) {
  4. /*uses external library*/ responsiveVoice.speak(
  5. '"' + words + '"',
  6. "UK English Male"
  7. );
  8. };
  9. let words = "hi";
  10. // USAGE:
  11. this.resetSharedVariables();
  12. fox.say(words);

Note: Nouns that have associated verbs (and nouns) are initialized as JS objects.

If we continue the conversation with the code above:

User: “fox mouth eats food”

CCC: “What is the initial value of food?”

User: “pests”

This conversation re-generates this updated JSON object:

  1. {
  2. "fox": {
  3. "say": "function(words) { /*uses external library*/responsiveVoice.speak('\"' + words + '\"', 'UK English Male'); }",
  4. "mouth": {
  5. "eat": "function(food) { alert(food); }"
  6. }
  7. },
  8. "words": "\"hi\"",
  9. "food": "\"pests\""
  10. }

Which re-generates this JS code:

  1. // VARIABLES and FUNCTIONS:
  2. let fox = {};
  3. fox.say = function (words) {
  4. /*uses external library*/ responsiveVoice.speak(
  5. '"' + words + '"',
  6. "UK English Male"
  7. );
  8. };
  9. fox.mouth = {};
  10. fox.mouth.eat = function (food) {
  11. alert(food);
  12. };
  13. let words = "hi";
  14. let food = "pests";
  15. // USAGE:
  16. this.resetSharedVariables();
  17. fox.say(words);
  18. fox.mouth.eat(food);

Built-in Things to Try:

Try these:

  • “move to a position” (when you’re prompted for a value, enter “top”/“bottom”/“middle”/etc., listed in the if block.)
  • “say something”
  • “what does the fox say”

https://github.com/hchiam/code-tutor