From 3ffa4ab123a3e6e99364a6f605764398736f0134 Mon Sep 17 00:00:00 2001 From: Ken Gorab Date: Sun, 14 Mar 2021 17:57:03 -0400 Subject: [PATCH] Cleaning up examples --- src/pages/PlaygroundPage/examples/enums.ts | 13 +- .../PlaygroundPage/examples/fibonacci.ts | 13 +- src/pages/PlaygroundPage/examples/fizzbuzz.ts | 13 +- src/pages/PlaygroundPage/examples/greeting.ts | 15 ++- .../examples/imports-exports.ts | 82 ++++++++++++ src/pages/PlaygroundPage/examples/index.ts | 119 ++---------------- .../PlaygroundPage/examples/linked-list.ts | 13 +- src/pages/PlaygroundPage/index.tsx | 2 +- 8 files changed, 146 insertions(+), 124 deletions(-) create mode 100644 src/pages/PlaygroundPage/examples/imports-exports.ts diff --git a/src/pages/PlaygroundPage/examples/enums.ts b/src/pages/PlaygroundPage/examples/enums.ts index d95b5fc..1835370 100644 --- a/src/pages/PlaygroundPage/examples/enums.ts +++ b/src/pages/PlaygroundPage/examples/enums.ts @@ -1,4 +1,10 @@ -export default ` +export default { + label: 'Enums', + modules: [ + { + name: '.enums', + label: 'enums.abra', + code: ` enum Color { Red Green @@ -13,4 +19,7 @@ enum Color { } Color.Red.hexCode() -`.trimStart() \ No newline at end of file +`.trimStart() + } + ] +} diff --git a/src/pages/PlaygroundPage/examples/fibonacci.ts b/src/pages/PlaygroundPage/examples/fibonacci.ts index a1e8e55..c9e2b97 100644 --- a/src/pages/PlaygroundPage/examples/fibonacci.ts +++ b/src/pages/PlaygroundPage/examples/fibonacci.ts @@ -1,4 +1,10 @@ -export default ` +export default { + label: 'Fibonacci', + modules: [ + { + name: '.fibonacci', + label: 'fibonacci.abra', + code: ` func fib(n: Int): Int { if (n == 0) { 0 @@ -10,4 +16,7 @@ func fib(n: Int): Int { } fib(12) -`.trimStart() \ No newline at end of file +`.trimStart() + } + ] +} diff --git a/src/pages/PlaygroundPage/examples/fizzbuzz.ts b/src/pages/PlaygroundPage/examples/fizzbuzz.ts index e1421ea..3407ed9 100644 --- a/src/pages/PlaygroundPage/examples/fizzbuzz.ts +++ b/src/pages/PlaygroundPage/examples/fizzbuzz.ts @@ -1,4 +1,10 @@ -export default ` +export default { + label: 'Fizzbuzz', + modules: [ + { + name: '.fizzbuzz', + label: 'fizzbuzz.abra', + code: ` for a in range(1, 101) { if a % 15 == 0 { println("Fizzbuzz") @@ -9,4 +15,7 @@ for a in range(1, 101) { } else { println(a) } -}`.trimStart() \ No newline at end of file +}`.trimStart() + } + ] +} diff --git a/src/pages/PlaygroundPage/examples/greeting.ts b/src/pages/PlaygroundPage/examples/greeting.ts index 6bea34b..72a6f60 100644 --- a/src/pages/PlaygroundPage/examples/greeting.ts +++ b/src/pages/PlaygroundPage/examples/greeting.ts @@ -1,8 +1,17 @@ -export default ` +export default { + label: 'Greeting', + modules: [ + { + name: '.greeting', + label: 'greeting.abra', + code: ` val greeting = "Hello" -func greet(recipient: String) = greeting + ", " + recipient +func greet(recipient: String): String = greeting + ", " + recipient val languageName = "Abra" greet(languageName) -`.trimStart() \ No newline at end of file +`.trimStart() + } + ] +} diff --git a/src/pages/PlaygroundPage/examples/imports-exports.ts b/src/pages/PlaygroundPage/examples/imports-exports.ts new file mode 100644 index 0000000..418b551 --- /dev/null +++ b/src/pages/PlaygroundPage/examples/imports-exports.ts @@ -0,0 +1,82 @@ +export default { + label: 'Imports/Exports', + modules: [ + { + name: '.main', + label: 'main.abra', + code: ` +import Ship from .ship +import Person from .person +import prettyPrint from .util + +val ship = Ship( + name: "Planet Express Ship", + crew: [ + Person(firstName: "Turanga", lastName: "Leela"), + Person(firstName: "Phillip", middleInitial: "J", lastName: "Fry"), + Person(firstName: "Bender", middleInitial: "B", lastName: "Rodriguez"), + ] +) + +println(prettyPrint(ship)) +`.trimStart() + }, + { + name: '.person', + label: 'person.abra', + code: ` +export type Person { + firstName: String + middleInitial: String? = None + lastName: String +} +`.trimStart() + }, + { + name: '.ship', + label: 'ship.abra', + code: ` +import Person from .person + +export type Ship { + name: String + crew: Person[] +} +`.trimStart() + }, + { + name: '.util', + label: 'util.abra', + code: ` +import Ship from .ship +import Person from .person + +// Ugly prettyPrint function, nothing to see here 🙈 +export func prettyPrint(ship: Ship): String { + var str = "Ship(\\n" + str += " name: \\"\${ship.name}\\",\\n" + str += " crew: [\\n" + for person, idx in ship.crew { + str += prettyPrintPerson(person, " ") + if idx != ship.crew.length - 1 { + str += "," + } + str += "\\n" + } + str += " ]\\n" + str += ")" +} + +func prettyPrintPerson(person: Person, indent = ""): String { + var str = "\${indent}Person(\\n" + str += "$indent firstName: \\"\${person.firstName}\\",\\n" + if person.middleInitial |i| { + str += "$indent middleInitial: \\"$i\\",\\n" + } + str += "$indent lastName: \\"\${person.lastName}\\",\\n" + str += "$indent)" +} +`.trimStart() + } + ] +} diff --git a/src/pages/PlaygroundPage/examples/index.ts b/src/pages/PlaygroundPage/examples/index.ts index c6b4355..233fa13 100644 --- a/src/pages/PlaygroundPage/examples/index.ts +++ b/src/pages/PlaygroundPage/examples/index.ts @@ -1,3 +1,4 @@ +import importsExports from './imports-exports' import greeting from './greeting' import fibonacci from './fibonacci' import fizzbuzz from './fizzbuzz' @@ -16,118 +17,12 @@ export interface Example { } const codeExamples: Example[] = [ - { - label: 'Greeting', - modules: [ - { name: '.greeting', label: 'greeting.abra', code: greeting } - ] - }, - { - label: 'Fibonacci', - modules: [ - { name: '.fibonacci', label: 'fibonacci.abra', code: fibonacci } - ] - }, - { - label: 'Fizzbuzz', - modules: [ - { name: '.fizzbuzz', label: 'fizzbuzz.abra', code: fizzbuzz } - ] - }, - { - label: 'Linked List (ish)', - modules: [ - { name: '.linked_list', label: 'linked_list.abra', code: linkedList } - ] - }, - { - label: 'Enums', - modules: [ - { name: '.enums', label: 'enums.abra', code: enums } - ] - }, - { - label: 'Imports/Exports', - modules: [ - { - name: '.main', - label: 'main.abra', - code: ` -import Ship from .ship -import Person from .person -import prettyPrint from .util - -val ship = Ship( - name: "Planet Express Ship", - crew: [ - Person(firstName: "Turanga", lastName: "Leela"), - Person(firstName: "Phillip", middleInitial: "J", lastName: "Fry"), - Person(firstName: "Bender", middleInitial: "B", lastName: "Rodriguez"), - ] -) - -println(prettyPrint(ship)) -`.trimStart() - }, - { - name: '.person', - label: 'person.abra', - code: ` -export type Person { - firstName: String - middleInitial: String? = None - lastName: String -} -`.trimStart() - }, - { - name: '.ship', - label: 'ship.abra', - code: ` -import Person from .person - -export type Ship { - name: String - crew: Person[] -} -`.trimStart() - }, - { - name: '.util', - label: 'util.abra', - code: ` -import Ship from .ship -import Person from .person - -// Ugly prettyPrint function, nothing to see here 🙈 -export func prettyPrint(ship: Ship): String { - var str = "Ship(\\n" - str += " name: \\"\${ship.name}\\",\\n" - str += " crew: [\\n" - for person, idx in ship.crew { - str += prettyPrintPerson(person, " ") - if idx != ship.crew.length - 1 { - str += "," - } - str += "\\n" - } - str += " ]\\n" - str += ")" -} - -func prettyPrintPerson(person: Person, indent = ""): String { - var str = "\${indent}Person(\\n" - str += "$indent firstName: \\"\${person.firstName}\\",\\n" - if person.middleInitial |i| { - str += "$indent middleInitial: \\"$i\\",\\n" - } - str += "$indent lastName: \\"\${person.lastName}\\",\\n" - str += "$indent)" -} -`.trimStart() - } - ] - } + importsExports, + greeting, + fibonacci, + fizzbuzz, + linkedList, + enums ] export default codeExamples diff --git a/src/pages/PlaygroundPage/examples/linked-list.ts b/src/pages/PlaygroundPage/examples/linked-list.ts index 8c90ebf..226d271 100644 --- a/src/pages/PlaygroundPage/examples/linked-list.ts +++ b/src/pages/PlaygroundPage/examples/linked-list.ts @@ -1,4 +1,10 @@ -export default ` +export default { + label: 'Linked List (ish)', + modules: [ + { + name: '.linked_list', + label: 'linked_list.abra', + code: ` type Node { value: T next: Node? = None @@ -45,4 +51,7 @@ list.push("a") .push("ghij") list.map(item => item.length) -`.trimStart() \ No newline at end of file +`.trimStart() + } + ] +} diff --git a/src/pages/PlaygroundPage/index.tsx b/src/pages/PlaygroundPage/index.tsx index 955010a..fe6564d 100644 --- a/src/pages/PlaygroundPage/index.tsx +++ b/src/pages/PlaygroundPage/index.tsx @@ -15,7 +15,7 @@ interface State { isError: boolean, } -const STARTING_EXAMPLE = examples[5] +const STARTING_EXAMPLE = examples[0] export default class PlaygroundPage extends React.Component<{}, State> { editorRef = React.createRef()