Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
0d3f61f
Align Selenium docs with the C++ compiler tree
ewanc26 Oct 25, 2026
1175ed2
Restore runtime support and harden Selenium CLI
ewanc26 Oct 25, 2026
fa1ca1a
Keep parser test source alive during tokenization
ewanc26 Oct 25, 2026
a0179bc
Run CLI smoke tests in CI via CTest
ewanc26 Oct 25, 2026
ab9fcf2
Cover numeric output in Selenium CLI smoke tests
ewanc26 Oct 25, 2026
71ed8ab
Document verified C++ compiler boundary and semantic-analysis gaps
ewanc26 Oct 25, 2026
0f81d3f
Correct stale runtime claim and link compiler status
ewanc26 Oct 25, 2026
d63abcf
Embed runtime helpers in generated Selenium C
ewanc26 Oct 25, 2026
8c7f06c
Test generated C independently of the source tree
ewanc26 Oct 25, 2026
5b2e025
Document self-contained Selenium C output
ewanc26 Oct 25, 2026
d63e24f
Define scoped semantic-analysis state
ewanc26 Oct 25, 2026
3fdeda3
Implement basic scoped semantic validation
ewanc26 Oct 25, 2026
ae4f096
Include semantic diagnostics exception header
ewanc26 Oct 25, 2026
19f7cc1
Test variable scopes and control-flow validation
ewanc26 Oct 25, 2026
ddf8740
Include Catch2 string matchers for semantic diagnostics
ewanc26 Oct 25, 2026
3f98b4a
Document implemented semantic checks and remaining gaps
ewanc26 Oct 25, 2026
d11f519
Update README for basic semantic analysis
ewanc26 Oct 25, 2026
d3ad52d
Remove unused parser condition flag
ewanc26 Oct 25, 2026
586396d
Remove unused code-emitter variable
ewanc26 Oct 25, 2026
fa4315b
Cover invalid continue, return, and duplicate parameters
ewanc26 Oct 25, 2026
dfc02ea
Restore global variable emitter and remove unused main-loop binding
ewanc26 Oct 25, 2026
8f04da8
Track declared functions for semantic validation
ewanc26 Oct 25, 2026
202e2a2
Validate function calls while supporting forward declarations
ewanc26 Oct 25, 2026
e483019
Test undeclared, forward, and recursive function calls
ewanc26 Oct 25, 2026
5c4bdfc
Use Selenium conditional keyword in recursive-call test
ewanc26 Oct 25, 2026
c743bce
Track function arities for call validation
ewanc26 Oct 25, 2026
dcc834a
Check function call arity and conflicting declarations
ewanc26 Oct 25, 2026
d0bae4d
Test semantic validation of function call arity
ewanc26 Oct 25, 2026
a6bd2e1
Document function-call semantic checks
ewanc26 Oct 25, 2026
e62761f
Mention function-call validation in compiler status
ewanc26 Oct 25, 2026
5bc143e
Track predeclared global variables in semantic analysis
ewanc26 Oct 25, 2026
bca61ef
Allow forward references to global variables and enum constants
ewanc26 Oct 25, 2026
8254b0e
Track predeclared enum constants separately
ewanc26 Oct 25, 2026
0a9bb33
Permit forward references to enum constants without double declaration
ewanc26 Oct 25, 2026
2db9c9d
Test forward global references and duplicate globals
ewanc26 Oct 25, 2026
62862b4
Keep duplicate-variable diagnostic consistent across scopes
ewanc26 Oct 25, 2026
3b00c15
Align duplicate-global regression diagnostic expectation
ewanc26 Oct 25, 2026
87e4321
Track function return types during semantic analysis
ewanc26 Oct 25, 2026
839d98a
Validate presence of return values against function return type
ewanc26 Oct 25, 2026
7894e9b
Test return-value presence against declared function type
ewanc26 Oct 25, 2026
40fff22
Document function return-value semantic checks
ewanc26 Oct 25, 2026
df3278e
Summarize current semantic-analysis checks in README
ewanc26 Oct 25, 2026
32d7999
Track function-scoped labels during semantic analysis
ewanc26 Oct 26, 2026
c7d7759
Validate goto targets and duplicate labels per function
ewanc26 Oct 26, 2026
904743f
Test goto target and label validation
ewanc26 Oct 26, 2026
3851c83
Document goto and label semantic checks
ewanc26 Oct 26, 2026
674ad34
Use valid labeled statements in duplicate-label test
ewanc26 Oct 26, 2026
ff60c7d
Document current semantic checks in README
ewanc26 Oct 26, 2026
0074974
Track function definitions separately from prototypes
ewanc26 Oct 26, 2026
4038084
Reject duplicate function definitions during semantic analysis
ewanc26 Oct 26, 2026
f40b4ff
Test duplicate function-definition diagnostics
ewanc26 Oct 26, 2026
97f15dd
Document duplicate function-definition diagnostics
ewanc26 Oct 26, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ jobs:
run: |
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j
- name: Unit tests
run: ./build/unit_tests
- name: Unit and CLI smoke tests
run: ctest --test-dir build --output-on-failure
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(catch2)

find_package(Python3 REQUIRED COMPONENTS Interpreter)

enable_testing()

add_executable(seleniumc
Expand All @@ -45,3 +47,4 @@ add_executable(unit_tests
target_include_directories(unit_tests PRIVATE src)
target_link_libraries(unit_tests PRIVATE Catch2::Catch2WithMain)
add_test(NAME unit_tests COMMAND unit_tests)
add_test(NAME cli_smoke COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tests/smoke.py $<TARGET_FILE:seleniumc> ${CMAKE_C_COMPILER})
30 changes: 18 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# Contributing to selenium
# Contributing to Selenium

an esoteric programming language with a lunar poetic theme that compiles to C.
Selenium is a C++20 compiler that lowers the .sel language to generated C.

## Project context
## Development setup

- Primary language: Python
- Default branch: main
A C++20 compiler, CMake, and a host C compiler are required. Catch2 is fetched by CMake during configuration.

## Before submitting changes
```sh
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build -j2
./build/unit_tests
```

- Read the README, manifests, and CI workflows before choosing commands.
- Run the documented formatter, linter, build, and test checks relevant to your change.
- Keep commits focused and explain compatibility or operational impact.
- Open changes through a pull request with verification results.
The current unit-test suite is not yet green on main: the parser hello-world test fails. End-to-end differential testing must be ported from the old Python module entry point to build/seleniumc; see https://github.com/ewanc26/selenium/issues/14.

## Recent direction
## Working rules

Recent commits: Sync AGENTS.md from zincfox; Sync AGENTS.md from zincfox; Sync AGENTS.md from zincfox; Sync CONTRIBUTING.md from zincfox; Sync CONTRIBUTING.md from zincfox
- Read the README, CMake build definition, compiler sources, and CI workflow before making changes.
- Preserve the lexer → parser → semantic analysis → C code generation pipeline.
- Update unit or differential tests when behavior changes.
- Do not claim a feature works merely because it appears in the language reference or capabilities.json.
- Keep README.md, AGENTS.md, CONTRIBUTING.md, CI, runtime paths, and the capability matrix aligned with the source that actually builds.
- Keep changes focused, and use a feature branch and pull request.
- Report the exact build and test commands run and their results.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,22 @@ whisper max;

## Project layout


```
selenium/
lexer.py – tokeniser (keywords, literals, operators)
parser.py – recursive-descent parser → AST
ast.py – AST node types
sema.py – type checker and semantic analyser
codegen_c.py – C code generator
main.py – CLI entry point
examples/ – sample .sel programs
CMakeLists.txt – C++20 compiler and unit-test targets
src/lexer/ – tokenizer and token definitions
src/parser/ – recursive-descent parser and AST construction
src/ast/ – AST node types
src/sema/ – semantic analysis (currently incomplete)
src/codegen/ – C source emitter
src/cli/ – seleniumc command-line driver
tests/unit/ – Catch2 unit tests
tests/differential/ – .sel programs paired with C reference programs
examples/ – sample .sel programs
```

The documented C++ build target is `build/seleniumc`. The runtime helpers are checked in, but the C++ migration still has gaps: semantic analysis now checks variable scopes, loop-control context, function declarations and call arity, return-value presence, and function-local goto/label targets, but type compatibility and several other semantic rules remain incomplete; the full differential suite has not been ported to the C++ executable. See [the C++ compiler status](docs/cpp-compiler-status.md) and [issue #14](https://github.com/ewanc26/selenium/issues/14).

## Notes

- The compiler is intentionally strict — no implicit type coercion.
Expand Down
29 changes: 29 additions & 0 deletions docs/cpp-compiler-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# C++ compiler status

This is an implementation-status note for the C++20 compiler, not a claim that the full language reference is validated end-to-end.

## Verified in the current CI smoke suite

- CMake builds the `seleniumc` compiler and Catch2 unit-test binary.
- `whisper "Hello, World!";` compiles to C and runs through the selected host C compiler.
- Numeric output is covered by the CLI smoke test.
- Generated C embeds the checked-in runtime helper implementation, so a saved C file can be compiled outside the repository without depending on a source-tree include path.
- `--run` uses a unique temporary directory, quotes shell arguments, and cleans temporary files.
- CLI errors for missing source files and unknown options are tested.

## Incomplete or not yet verified

- `src/sema/sema.cpp` now checks variable declarations/references across nested scopes, duplicate variables/parameters, undeclared function calls, duplicate function definitions, conflicting function arities, wrong call argument counts, `return` outside functions, and `break`/`continue` context. Type compatibility, function parameter types, lvalue validity, and full control-flow analysis are still incomplete. Goto targets are checked within function boundaries, and duplicate labels are rejected.
- The full differential suite still points at the removed Python package and has not been ported to the C++ executable.
- The C++ parser/code generator contains broader syntax than the smoke suite currently verifies. Do not treat the README language reference or `capabilities.json` as proof of full feature parity.
- Runtime behavior for arrays, pointers, aggregates, FFI, type conversions, and advanced control flow requires focused C++ end-to-end regression tests before being marked complete.

## Verification

```sh
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build -j2
ctest --test-dir build --output-on-failure
```

The current pull request runs these checks in CI. Full semantic analysis and C++ differential coverage remain tracked in [issue #14](https://github.com/ewanc26/selenium/issues/14).
29 changes: 29 additions & 0 deletions runtime/selenium_runtime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdbool.h>
#include <stdio.h>

void selenium_print_int(int value) { printf("%d\n", value); }
void selenium_print_float(double value) { printf("%.15g\n", value); }
void selenium_print_bool(_Bool value) { puts(value ? "true" : "false"); }
void selenium_print_char(char value) { putchar((unsigned char)value); putchar('\n'); }
void selenium_print_string(const char *value) { puts(value ? value : "(null)"); }

int selenium_read_int(void) {
int value = 0;
if (scanf("%d", &value) != 1) return 0;
return value;
}
double selenium_read_float(void) {
double value = 0.0;
if (scanf("%lf", &value) != 1) return 0.0;
return value;
}
_Bool selenium_read_bool(void) {
int value = 0;
if (scanf("%d", &value) != 1) return false;
return value != 0;
}
char selenium_read_char(void) {
char value = '\0';
if (scanf(" %c", &value) != 1) return '\0';
return value;
}
202 changes: 158 additions & 44 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,73 @@
#include "../parser/parser.h"
#include "../sema/sema.h"
#include "../codegen/c_emitter.h"
#include <iostream>
#include <fstream>
#include <sstream>

#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <system_error>
#include <sys/wait.h>
#include <utility>

#ifndef SELENIUM_RUNTIME_C
#error "SELENIUM_RUNTIME_C must be defined"
#endif

namespace {

std::string shell_quote(const std::string &value) {
std::string quoted = "'";
for (char c : value) {
if (c == '\'') quoted += "'\\''";
else quoted += c;
}
quoted += "'";
return quoted;
}

std::filesystem::path create_temp_directory() {
const auto base = std::filesystem::temp_directory_path();
const auto ticks = std::chrono::steady_clock::now().time_since_epoch().count();
std::random_device random;
for (int attempt = 0; attempt < 16; ++attempt) {
const auto candidate = base / ("selenium-" + std::to_string(ticks) + "-" +
std::to_string(random()));
std::error_code error;
if (std::filesystem::create_directory(candidate, error)) return candidate;
if (error && error != std::errc::file_exists) {
throw std::runtime_error("cannot create temporary directory: " + error.message());
}
}
throw std::runtime_error("cannot create a unique temporary directory for --run");
}

struct TemporaryDirectory {
explicit TemporaryDirectory(std::filesystem::path value) : path(std::move(value)) {}
~TemporaryDirectory() {
std::error_code ignored;
std::filesystem::remove_all(path, ignored);
}
std::filesystem::path path;
};

int shell_status(int status) {
if (status == -1) return 1;
if (WIFEXITED(status)) return WEXITSTATUS(status);
if (WIFSIGNALED(status)) return 128 + WTERMSIG(status);
return 1;
}

} // namespace

int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "usage: seleniumc <file.sel> [-o output] [--run] [--cc gcc]\n";
std::cerr << "usage: seleniumc <file.sel> [-o <output.c> | --run] [--cc <compiler>]\n";
return 1;
}
if (std::string(argv[1]) == "--support") {
Expand All @@ -23,56 +77,116 @@ int main(int argc, char **argv) {
std::cout << " GitHub Sponsors: https://github.com/sponsors/ewanc26\n";
return 0;
}
std::string input = argv[1];

const std::string input = argv[1];
std::string output;
bool run = false;
std::string cc = "gcc";
for (int i = 2; i < argc; i++) {
std::string a = argv[i];
if (a == "-o" && i+1 < argc) output = argv[++i];
else if (a == "--run") run = true;
else if (a == "--cc" && i+1 < argc) cc = argv[++i];
bool run = false;
for (int i = 2; i < argc; ++i) {
const std::string arg = argv[i];
if (arg == "-o" || arg == "--output") {
if (i + 1 >= argc) {
std::cerr << "seleniumc: " << arg << " requires a path\n";
return 1;
}
output = argv[++i];
} else if (arg == "--run") {
run = true;
} else if (arg == "--cc") {
if (i + 1 >= argc) {
std::cerr << "seleniumc: --cc requires a compiler executable path\n";
return 1;
}
cc = argv[++i];
} else {
std::cerr << "seleniumc: unknown option " << arg << "\n";
return 1;
}
}
if (!run && output.empty()) { std::cerr << "Either -o/--output or --run must be specified\n"; return 1; }

if (run && !output.empty()) {
std::cerr << "seleniumc: choose either --run or -o/--output, not both\n";
return 1;
}
if (!run && output.empty()) {
std::cerr << "seleniumc: specify -o <output.c> or --run\n";
return 1;
}

std::ifstream in(input);
if (!in) { std::cerr << "cannot open file\n"; return 1; }
std::stringstream ss; ss << in.rdbuf();
std::string source = ss.str();

if (!in) {
std::cerr << "seleniumc: cannot read source file: " << input << "\n";
return 1;
}
std::stringstream source_stream;
source_stream << in.rdbuf();
const std::string source = source_stream.str();

try {
Tokenizer t(source);
auto tokens = t.tokenize();
Parser p(tokens);
auto prog = p.parse();
Tokenizer tokenizer(source);
const auto tokens = tokenizer.tokenize();
Parser parser(tokens);
auto program = parser.parse();
SemanticAnalyzer sema;
sema.analyze(prog);
std::string ccode = emitProgram(prog);

if (run) {
// write temp files
std::filesystem::path tmpdir = std::filesystem::temp_directory_path() / "selenium_XXXXXX";
std::string tmpdirstr = tmpdir.string();
// simple approach
std::string cfile = "/tmp/selenium_out.c";
std::string exefile = "/tmp/selenium_out";
std::ofstream out(cfile);
out << ccode;
out << "\n#include \"" << SELENIUM_RUNTIME_C << "\"\n";
out.close();
std::string cmd = cc + " " + cfile + " -o " + exefile + " -lm 2>&1";
int r = system(cmd.c_str());
if (r != 0) return 1;
r = system(exefile.c_str());
return WEXITSTATUS(r);
} else {
sema.analyze(program);
std::string ccode = emitProgram(program);
std::ifstream runtime_file(SELENIUM_RUNTIME_C);
if (!runtime_file) {
throw std::runtime_error("cannot read Selenium runtime source: " +
std::string(SELENIUM_RUNTIME_C));
}
std::stringstream runtime_stream;
runtime_stream << runtime_file.rdbuf();
ccode += "\n/* Embedded Selenium runtime helpers. */\n";
ccode += runtime_stream.str();
ccode += "\n";

if (!run) {
std::ofstream out(output);
if (!out) {
std::cerr << "seleniumc: cannot create output file: " << output << "\n";
return 1;
}
out << ccode;
out << "\n#include \"" << SELENIUM_RUNTIME_C << "\"\n";
if (!out) {
std::cerr << "seleniumc: failed while writing output file: " << output << "\n";
return 1;
}
return 0;
}

TemporaryDirectory temp(create_temp_directory());
const auto cfile = (temp.path / "selenium_out.c").string();
const auto exefile = (temp.path / "selenium_out").string();
{
std::ofstream generated(cfile);
if (!generated) {
std::cerr << "seleniumc: cannot create temporary C source\n";
return 1;
}
generated << ccode;
if (!generated) {
std::cerr << "seleniumc: failed while writing temporary C source\n";
return 1;
}
}

const std::string compile_command = shell_quote(cc) + " " + shell_quote(cfile) +
" -o " + shell_quote(exefile) + " -lm";
const int compile_status = std::system(compile_command.c_str());
if (compile_status == -1) {
std::cerr << "seleniumc: failed to start host C compiler\n";
return 1;
}
if (shell_status(compile_status) != 0) return shell_status(compile_status);

const int run_status = std::system(shell_quote(exefile).c_str());
if (run_status == -1) {
std::cerr << "seleniumc: failed to start generated program\n";
return 1;
}
} catch (std::exception &e) {
return shell_status(run_status);
} catch (const std::exception &e) {
std::cerr << "seleniumc: " << e.what() << "\n";
return 1;
}
return 0;
}
3 changes: 1 addition & 2 deletions src/codegen/c_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,7 @@ std::string emitProgram(const Program &prog) {
ss << "int main() {\n";
for (auto &it : others) {
if (std::holds_alternative<VarDeclStmt>(it->node)) {
auto &v = std::get<VarDeclStmt>(it->node);
// only if not global? we put globals above; but need to handle - in our model globals become globals if top level
// Top-level variables were emitted as globals above.
// for now skip duplicates? or emit
} else if (std::holds_alternative<FunctionDeclStmt>(it->node)) continue;
else if (std::holds_alternative<StructDeclStmt>(it->node) || std::holds_alternative<UnionDeclStmt>(it->node) ||
Expand Down
Loading
Loading