Added a skeleton for the lexer
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1 +1,2 @@
 | 
			
		||||
build/
 | 
			
		||||
build/*
 | 
			
		||||
.cache
 | 
			
		||||
@@ -1,3 +1,10 @@
 | 
			
		||||
cmake_minimum_required(VERSION 3.16)
 | 
			
		||||
project(lispy_stuff)
 | 
			
		||||
 | 
			
		||||
add_executable(main src/main.cpp)
 | 
			
		||||
set(HEADER_FILES src/include/lex.hpp)
 | 
			
		||||
set(SOURCE_FILES src/main.cpp)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
add_executable(main ${SOURCE_FILES} ${HEADER_FILES})
 | 
			
		||||
 | 
			
		||||
target_include_directories(main PRIVATE src/include/)
 | 
			
		||||
							
								
								
									
										31
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
# Lispy stuff
 | 
			
		||||
 | 
			
		||||
Simple lisp-ish language compiler written in C++.
 | 
			
		||||
 | 
			
		||||
Right now it doesn't compile much - it's just a parser.
 | 
			
		||||
The goal is to emit bytecode. The bytecode format is not decided yet.
 | 
			
		||||
 | 
			
		||||
## Build
 | 
			
		||||
 | 
			
		||||
I use cmake for the build system. I prefer to build out-of-tree,
 | 
			
		||||
here's how to build if you've never used cmake:
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
cd build
 | 
			
		||||
cmake ..
 | 
			
		||||
make
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Development
 | 
			
		||||
 | 
			
		||||
I use clangd as the language server. Appropriate `compile_commands.json`
 | 
			
		||||
(required for clangd, otherwise it can not find include files) is provided.
 | 
			
		||||
If you'd like to generate them yourself, just use cmake:
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
cd build
 | 
			
		||||
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ..
 | 
			
		||||
cp compile_commands.json ../
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										8
									
								
								compile_commands.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								compile_commands.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
[
 | 
			
		||||
{
 | 
			
		||||
  "directory": "/home/haxala1r/Desktop/Programming/C++/lispy-stuff/build",
 | 
			
		||||
  "command": "/usr/bin/c++  -I/home/haxala1r/Desktop/Programming/C++/lispy-stuff/src/include  -o CMakeFiles/main.dir/src/main.cpp.o -c /home/haxala1r/Desktop/Programming/C++/lispy-stuff/src/main.cpp",
 | 
			
		||||
  "file": "/home/haxala1r/Desktop/Programming/C++/lispy-stuff/src/main.cpp",
 | 
			
		||||
  "output": "CMakeFiles/main.dir/src/main.cpp.o"
 | 
			
		||||
}
 | 
			
		||||
]
 | 
			
		||||
							
								
								
									
										36
									
								
								src/include/lex.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/include/lex.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <variant>
 | 
			
		||||
 | 
			
		||||
enum TokenType {
 | 
			
		||||
    OpenParen,
 | 
			
		||||
    CloseParen,
 | 
			
		||||
    Symbol,
 | 
			
		||||
    String,
 | 
			
		||||
    Int,
 | 
			
		||||
    End
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Plain Old Data
 | 
			
		||||
struct Token {
 | 
			
		||||
    enum TokenType type;
 | 
			
		||||
    std::variant<int, std::string> value;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class Lexer {
 | 
			
		||||
private:
 | 
			
		||||
    // we use a stringstream for lexing purposes
 | 
			
		||||
    std::stringstream ss;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    Lexer(std::string);
 | 
			
		||||
    Lexer();
 | 
			
		||||
 | 
			
		||||
    void feed(std::string);
 | 
			
		||||
    
 | 
			
		||||
    Token next();
 | 
			
		||||
    std::vector<Token> collect();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										16
									
								
								src/lex.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/lex.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
#include <lex.hpp>
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
Lexer::Lexer(std::string s) : ss(s) {}
 | 
			
		||||
Lexer::Lexer() : ss("") {}
 | 
			
		||||
 | 
			
		||||
void Lexer::feed(std::string s) {
 | 
			
		||||
    ss << s;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Token Lexer::next() {
 | 
			
		||||
    return {TokenType::CloseParen};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -1,4 +1,6 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <lex.hpp>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user