Skip to content

Commit

Permalink
feat: add single line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismael Matsinhe committed Jun 18, 2023
1 parent dc510ab commit b3e57de
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ print("2 + 3 = ", result);

In this example, the `adder` function returns a closure that adds the provided value `x` to any value `y` passed to it. The closure `addTwo` adds 2 to its argument `3` and returns the result `5`.

### Single-line Comments Example

```sml
// This is a single-line comment.
# This is also a single-line comment.
```
### Array Example

```sml
Expand Down
2 changes: 1 addition & 1 deletion evaluator/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var builtins = map[string]*object.Builtin{
"input": {Fn: inputFunc},
"int": {Fn: intFunc},
"str": {Fn: strFunc},
// file system

"readFile": {Fn: readFileFunc},
"writeFile": {Fn: writeFileFunc},
"removeFile": {Fn: removeFileFunc},
Expand Down
12 changes: 10 additions & 2 deletions hello.sa
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
let val_10 = 10;
print("hello Jose \n Im glad you came to my class");
let adder = fn(x) {
fn(y) {
x + y;
};
};
// adder is a function that returns a function
// addTwo is a function that takes a number and returns a number
let addTwo = adder(2);
let result = addTwo(3);
//print("2 + 3 = ", result);
14 changes: 14 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ func (l *Lexer) NextToken() token.Token {
case '-':
tok = newToken(token.MINUS, l.ch)
case '/':
// check for comment
if l.peekChar() == '/' {
l.readChar()
l.skipComment()
return l.NextToken()
}
tok = newToken(token.SLASH, l.ch)
case '#':
l.skipComment()
return l.NextToken()
case '*':
tok = newToken(token.ASTERISK, l.ch)
case '<':
Expand Down Expand Up @@ -128,6 +137,11 @@ func (l *Lexer) readIdentifier() string {
return l.input[position:l.position]
}

func (l *Lexer) skipComment() {
for l.ch != '\n' && l.ch != 0 {
l.readChar()
}
}
func newToken(tokenType token.TokenType, ch byte) token.Token {
return token.Token{Type: tokenType, Literal: string(ch)}
}
Expand Down
Binary file removed samora
Binary file not shown.

0 comments on commit b3e57de

Please sign in to comment.