Skip to content

Commit

Permalink
Added readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoucy committed Aug 24, 2024
1 parent 94e4d84 commit a2d4932
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
.idea/
cmake-build-*/
interpreter
hudson-interpreter
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(CMAKE_CXX_FLAGS "-std=c++20 -Wpedantic -Wall -Wextra ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g3 ${CMAKE_CXX_FLAGS_DEBUG}")

if (${CMAKE_BUILD_TYPE} STREQUAL "test")
add_subdirectory(tst)
add_subdirectory(tests)
return()
endif()

Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ all: build
build:
cmake -DCMAKE_BUILD_TYPE=debug -G "Unix Makefiles" -B cmake-build-debug
make -C cmake-build-debug
cp cmake-build-debug/interpreter .
cp cmake-build-debug/interpreter ./hudson-interpreter

test:
cmake -DCMAKE_BUILD_TYPE=test -G "Unix Makefiles" -B cmake-build-test
make -C cmake-build-test

run-test: test
./cmake-build-test/tst/interpreter-test
./cmake-build-test/tests/interpreter-test

coverage:
gcovr -e "tst/" -e "cmake-build-test/"

clean-build:
rm -rf cmake-build-debug
rm -f ./interpreter
rm -f ./hudson-interpreter

clean-test:
rm -rf cmake-build-test
Expand Down
189 changes: 186 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,187 @@
[![Continuous Integration](https://github.com/cdoucy/interpreter/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/cdoucy/interpreter/actions/workflows/ci.yaml)
[![Unit tests](https://github.com/cdoucy/interpreter/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/cdoucy/interpreter/actions/workflows/ci.yaml)

# interpreter
Interpreter implementation in C++
# Hudson Programming Language

Welcome to the Hudson programming language! Hudson is a simple, yet powerful, programming language. This project is designed to be a learning tool for both language design and implementation, focusing on a clear, minimalistic syntax and core programming constructs.

This repository contains the complete specification of the Hudson programming language and the implementation of its interpreter in C++.

## Table of Contents

- [Overview](#overview)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [Testing](#examples)
- [Grammar](#grammar)
- [Contributing](#contributing)
- [License](#license)

## Overview

Hudson is a programming language that draws inspiration from several other languages while maintaining its own unique approach to syntax and semantics. It is statically typed and imperative, with a focus on simplicity and readability. This project aims to provide a foundation for further exploration into language design and interpreter construction.

## Features

- **Simple Syntax**: Hudson has a clean and minimalistic syntax that is easy to understand.
- **Basic Control Structures**: Supports `if`, `while`, `for` loops, and `break`/`continue` statements.
- **Functions**: Allows defining and invoking functions, including anonymous functions.
- **Expressions**: Includes a variety of expressions with support for logical, bitwise, and arithmetic operations.
- **Variable Declarations and Assignments**: Supports variable declarations with optional initialization and compound assignment operations.
- **Increment and Decrement Operators**: `++` and `--` are supported for quick variable updates.
- **Printing**: Built-in `print` statement for output.

## Installation

To build and run the Hudson language interpreter:

1. **Clone the repository**:
```bash
git clone https://github.com/cdoucy/hudson-lang.git
cd hudson-lang
```

2. **Build the project**:
Ensure you have CMake installed, then run:
```bash
make
```
This will generate the Hudson interpreter executable.

3. **Run a Hudson program**:
```bash
./hudson-interpreter examples/hello_world.hu
```

## Usage

You can create Hudson source files with the `.hu` extension and run them using the Hudson interpreter. Here's a basic example of a Hudson program:

```hudson
int x = 10;
int y = 20;
print(x + y);
```

## Examples

Here are some examples of what you can do in Hudson:

### Hello World

```hudson
print("Hello, World!");
```

### Loops and Conditionals

```hudson
int i = 0;
while (i < 5) {
print(i);
i++;
}
if (i == 5) {
print("Done!");
} else {
print("Something went wrong.");
}
```

### Functions and Recursion

```hudson
fnc factorial(n int) int {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int result = factorial(5);
print(result);
```

## Testing

The Hudson programming language interpreter has been developed using Test-Driven Development (TDD) to ensure high-quality and reliable code. You can run unit tests and check code coverage with the following commands:

1. **Run the Unit Tests**:
To run all the unit tests for the Hudson interpreter, use the following command:

```bash
make run-test
```
This command will execute all unit tests and display the results in the terminal.

2. **Check Coverage**: To check the code coverage of the unit tests, use the command:
To run all the unit tests for the Hudson interpreter, use the following command:

```bash
make coverage
```
This command will generate a coverage report, showing how much of the codebase is covered by the tests. Reviewing this report can help identify untested parts of the code and ensure comprehensive test coverage.

## Grammar

The specification of the Hudson programming language in EBNF (Extended Backus-Naur Form) notation is as follows:

```ebnf
program = { stmt }
stmt = expr_stmt | declaration | assignment | inc_dec | return
| break | continue | print | block | while
| for | if | function
expr_stmt = expr ";"
declaration = type ident [ "=" expr ] ";"
assignment = ident ( "=" | "+=" | "+-" | "*=" | "%=" | "/+" ) expr ";"
inc_dec = ident ( "++" | "--" ) ";"
return = "return" [ expr ] ";"
break = "break" ";"
continue = "continue" ";"
print = "print" "(" [ expr ] ")" ";"
block = "{" { stmt } "}"
while = "while" "(" expr ")" ( stmt | ";" )
for = "for" "(" [ declaration | assignment ] ";" expr ";" assignment | inc_dec ")" ( smt | ";" )
if = "if" "(" expr ")" stmt [ "else" stmt ]
function = "fnc" ident fnc_signature block
expr = or
or = and { "||" and }
and = bitwise_or { "&&" bitwise_or }
bitwise_or = bitwise_xor { "|" bitwise_xor }
bitwise_xor = bitwise_and { "^" bitwise_and }
bitwise_and = equality { "&" equality }
equality = comparison { "==" | "!=" comparison }
comparison = bitshift { "<" | "<=" | ">" | ">=" bitshift }
bitshift = term { ">>" | "<<" term }
term = factor { "-" | "+" factor }
factor = unary { "/" | "*" | "%" unary }
unary = ( "+" | "-" | "!" | "~" ) unary | call
call = primary { "(" [ params ] ")" }
primary = int_lit | ident | string_lit | anonymous_fnc | grouping
grouping = "(" expr ")"
params = expr { "," expr }
anonymous_fnc = 'fnc' fnc_signature block
fnc_signature = "(" [ ident type { "," ident type } ] ")" [ type ]
type = "int" | "str" | "fnc" | fnc_signature
ident = ( letter | "_" ) { letter | digit | "_" }
int_lit = digit | { digit }
string_lit = '"' { * } '"'
letter = "A" ... " Z" | "a" ... "z"
digit = "0" ... "9"
```

## Credits

This project was inspired by the book [Crafting Interpreters](https://craftinginterpreters.com/) by Robert Nystrom. The book provided valuable insights into the design and implementation of interpreters, which greatly influenced the development of the Hudson programming language.


## Contributing

Contributions are welcome! If you have ideas for improvements or new features, feel free to open an issue or submit a pull request. Please make sure to follow the coding standards used in the project and include tests where applicable.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
48 changes: 0 additions & 48 deletions doc/grammar.md

This file was deleted.

1 change: 1 addition & 0 deletions examples/hello_world.hu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello world!");
2 changes: 1 addition & 1 deletion tst/CMakeLists.txt → tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set(TARGET interpreter-test)
set (PROJECT_ROOT ./..)
include(${PROJECT_ROOT}/src/src.cmake)
include(${PROJECT_ROOT}/inc/inc.cmake)
include(${PROJECT_ROOT}/tst/tst.cmake)
include(${PROJECT_ROOT}/tests/tst.cmake)

include(FetchContent)
FetchContent_Declare(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/tst.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set(
TEST_SOURCES

${PROJECT_ROOT}/tests/lexer/lexer_test.cpp
${PROJECT_ROOT}/tests/evaluator/evaluator_test.cpp
${PROJECT_ROOT}/tests/evaluator/statements_test.cpp
${PROJECT_ROOT}/tests/parser/parser_test.cpp
${PROJECT_ROOT}/tests/parser/parser_error_test.cpp
)
9 changes: 0 additions & 9 deletions tst/tst.cmake

This file was deleted.

0 comments on commit a2d4932

Please sign in to comment.