Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lexer tokenize the hello world program the right way

  • Loading branch information...
commit f3c2de265065f0abe0811141d909510eef296094 1 parent c0d812b
@thesp0nge authored
View
8 README.md
@@ -38,7 +38,7 @@ Let's look at our first coat program.
``` hello_world.coat
\# As in ruby, this is a comment. We will follow the same naming convention
\# used in ruby to translate filenames in class.
-contract "HelloWorld":
+contract HelloWorld:
\# Classes can have pre and post conditions as well.
\# Pre conditions can be environment variables or command line switches
@@ -61,8 +61,10 @@ contract "HelloWorld":
\#
\# Remember, you must look for hello_world_spec.rb and use BDD.
say_hello
- pre: none
- post: "Hello world"
+ pre:
+ none
+ post:
+ "Hello world"
```
This would be the contract for an "Hello world" project.
View
3  lib/coat/grammar.y
@@ -15,7 +15,7 @@ token API
token IDENTIFIER
token CONSTANT
token INDENT DEDENT
-token TRUE FALSE NIL
+token TRUE FALSE NIL NONE
rule
# All rules are declared in this format:
@@ -69,6 +69,7 @@ rule
| TRUE { result = TrueNode.new }
| FALSE { result = FalseNode.new }
| NIL { result = NilNode.new }
+ | NONE { result = NoneNode.new }
;
# The contract definition
View
2  lib/coat/lexer.rb
@@ -1,6 +1,6 @@
module Coat
class Lexer
- KEYWORDS = ["pre", "contract", "post", "read", "from", "stdout", "stderr", "api"]
+ KEYWORDS = ["none", "pre", "contract", "post", "read", "from", "stdout", "stderr", "api"]
def tokenize(code)
# Cleanup code by remove extra line breaks
View
5 lib/coat/nodes.rb
@@ -26,6 +26,11 @@ def initialize
super(nil)
end
end
+class NoneNode < LiteralNode
+ def initialize
+ super("none")
+ end
+end
# Retrieving the value of a constant.
class GetConstantNode < Struct.new(:name); end
View
28 spec/lexer_spec.rb
@@ -48,7 +48,33 @@
end
it "must recognize the hello world contract " do
- fail "you must write it"
+code= <<-CODE
+contract HelloWorld:
+ pre:
+ none
+ post:
+ read "Hello world" from stdout
+ api:
+ say_hello:
+ pre:
+ none
+ post:
+ "Hello world"
+CODE
+ tokens = [
+ [:CONTRACT, "contract"], [:CONSTANT, "HelloWorld"],
+ [:INDENT, 2], [:PRE, "pre"],
+ [:INDENT, 4], [:NONE, "none"], [:DEDENT, 2], [:NEWLINE, "\n"],
+ [:POST, "post"],
+ [:INDENT, 4], [:READ, "read"], [:STRING, "Hello world"], [:FROM, "from"], [:STDOUT, "stdout"], [:DEDENT, 2], [:NEWLINE, "\n"],
+ [:API, "api"],
+ [:INDENT, 4], [:IDENTIFIER, "say_hello"],
+ [:INDENT, 6], [:PRE, "pre"],
+ [:INDENT, 8], [:NONE, "none"], [:DEDENT, 6], [:NEWLINE, "\n"],
+ [:POST, "post"],
+ [:INDENT, 8], [:STRING, "Hello world"], [:DEDENT, 2], [:DEDENT, 2], [:DEDENT, 2], [:DEDENT, 0]
+ ]
+ lexer.tokenize(code).should =~ tokens
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.