Skip to content

Commit dd70d96

Browse files
committed
changed up the CMakeLists.txt
1 parent 1068347 commit dd70d96

10 files changed

Lines changed: 113 additions & 17 deletions

‎CMakeLists.txt‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ project(basiclang)
33

44
set(CMAKE_CXX_STANDARD 26)
55

6-
find_package(Boost REQUIRED)
6+
# this part is vibecoded
7+
include(FetchContent)
8+
foreach(dep multiprecision config assert)
9+
FetchContent_Declare(boost_${dep}
10+
GIT_REPOSITORY https://github.com{dep}.git
11+
GIT_TAG boost-1.85.0
12+
GIT_SHALLOW ON
13+
)
14+
endforeach()
15+
FetchContent_MakeAvailable(boost_multiprecision boost_config boost_assert)
716

817

918
add_executable(basiclang main.cpp
@@ -17,7 +26,12 @@ add_executable(basiclang main.cpp
1726
bytecode.h
1827
)
1928

20-
target_link_libraries(basiclang PRIVATE Boost::headers)
29+
# this part is vibecoded too
30+
31+
target_include_directories(basiclang PRIVATE
32+
${boost_multiprecision_SOURCE_DIR}/include
33+
${boost_config_SOURCE_DIR}/include
34+
${boost_assert_SOURCE_DIR}/include
35+
)
2136

22-
# 4. Keep Standalone Mode active so it stays decoupled from compiled binaries
2337
target_compile_definitions(basiclang PRIVATE BOOST_MP_STANDALONE)

‎bytecode.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void vm::Vm::Run() const {
4040
const auto &instruction = m_Code[idx];
4141
switch (instruction.Type) {
4242
case ByteCodeType::LoadConst: {
43-
auto& [val] = static_cast<const BigIntData &>(*instruction.LoadedData);
43+
const auto& [val] = static_cast<const BigIntData &>(*instruction.LoadedData);
4444
stack.emplace_back(val);
4545
}
4646
break;
@@ -67,14 +67,12 @@ void vm::Vm::Run() const {
6767
auto& [ so ] = static_cast<const UintData &>(*instruction.LoadedData);
6868
auto condition = GetBack();
6969

70-
71-
// std::cout << "is " << condition << " == 0? if so, go to " << so << ", else " << els << '\n';
7270
if (condition) idx = m_Labels.at(so)-1; // if
7371

7472
}
7573
break;
7674
case ByteCodeType::Out: {
77-
auto [s] = static_cast<const StringData &>(*instruction.LoadedData);
75+
const auto [s] = static_cast<const StringData &>(*instruction.LoadedData);
7876
RunOutput(stack, s);
7977
}
8078
break;
@@ -160,6 +158,7 @@ void vm::Vm::Run() const {
160158

161159
stack.push_back(a ^ b);
162160
}
161+
break;
163162
case ByteCodeType::Div: {
164163

165164
auto b = GetBack();
@@ -204,6 +203,9 @@ void vm::Vm::RunOutput(std::vector<BigInt> &stack, std::string_view format) {
204203
} else if (isEscaped && ch == 'n') {
205204
result += '\n';
206205
isEscaped = false;
206+
} else if (isEscaped && ch == '"') {
207+
result += '"';
208+
isEscaped = false;
207209
} else if (isEscaped && ch == '\\') {
208210
result += '\\';
209211
isEscaped = false;

‎code.sashko‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎lexer.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ static constexpr Lexer::Token makeString(std::string_view source) {
104104
}
105105
if (result[0] == '\\' && !escaped) {
106106
escaped = true;
107+
} else if (escaped) {
108+
escaped = false;
107109
}
108110
result.remove_prefix(1);
109111
}
@@ -116,7 +118,7 @@ Lexer::Token Lexer::Lexer::Peek(int *lineBatch) const {
116118
auto source = m_Source;
117119

118120
tokenize:
119-
source = stripWhitespace(source);
121+
// source = stripWhitespace(source);
120122
if (source.empty()) return Token{.Type = TokenType::eof, .Literal = "", .Line = m_Line};
121123

122124

@@ -189,6 +191,7 @@ Lexer::Token Lexer::Lexer::Peek(int *lineBatch) const {
189191
}
190192

191193
Lexer::Token Lexer::Lexer::Next() {
194+
m_Source = stripWhitespace(m_Source);
192195
int lineBatch = 0;
193196
std::optional token = Peek(&lineBatch);
194197
m_Line += lineBatch;

‎lexer.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace Lexer {
6161
};
6262

6363
class Lexer {
64-
std::string_view m_Source;
64+
std::string_view m_Source;
6565
size_t m_Line = 1;
6666
int m_Parenthesis = 0; // for balancing parenthesis'. Used for multiline stuff.
6767
int m_Interpolations = 0; // for making sure you can start a string there.
@@ -70,7 +70,7 @@ namespace Lexer {
7070

7171
public:
7272
// ReSharper disable once CppNonExplicitConvertingConstructor
73-
Lexer(std::string_view source) : m_Source(source) {
73+
Lexer( std::string_view source) : m_Source(source) {
7474
}
7575

7676
[[nodiscard]] Token Peek(int *lineBatch = nullptr) const;

‎main.cpp‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88

99
int main(int argc, char** argv) {
10-
Log::GlobalLogLevel = Log::Level::Info;
10+
Log::GlobalLogLevel = Log::Level::Error;
1111

12+
13+
14+
const char* name = argv[1];
1215
if (argc < 2) {
16+
// name = "/Users/sashko/mystuff/progfrog/cp/basiclang/maxNumDemo.basiclang";
1317
std::println("{} <file_name>", argv[0]);
1418
return -1;
1519
}
16-
std::ifstream file{argv[1]};
20+
std::ifstream file{name};
1721

1822

1923
std::ostringstream ss;
@@ -22,6 +26,14 @@ int main(int argc, char** argv) {
2226
// std::println("{}", source);
2327

2428
Lexer::Lexer l{source};
29+
30+
// auto clone = l;
31+
// Lexer::Token t = clone.Next();
32+
// while (t.Type != Lexer::TokenType::eof) {
33+
// std::println("{}", t.Description());
34+
// t = clone.Next();
35+
// }
36+
2537
Parser::Parser p{l};
2638

2739
p.parseProgram();

‎maxNumDemo.basiclang‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
out "|a| = "
2+
in "{n}"
3+
4+
out "a: "
5+
6+
in "{res}"
7+
8+
i = 1
9+
loop:
10+
in "{num}"
11+
if((num <=> res) - 1) res = num
12+
13+
i = i+1
14+
if ((i <=> n) + 1) goto loop
15+
16+
out "\nmax(a) = {res}.\n"

‎parser.cpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ namespace Parser {
3939
if (auto cur = Current(); cur.Type == t) {
4040
return;
4141
}
42-
logger.Log<Log::Level::Error>("Expected a token of {} type, a {} found",
42+
logger.Log<Log::Level::Error>("Expected a token of {} type, a {} found (on line {})",
4343
static_cast<int>(t),
44-
static_cast<int>(Current().Type)
44+
static_cast<int>(Current().Type),
45+
Current().Line
4546
);
4647
}
4748

@@ -56,7 +57,7 @@ namespace Parser {
5657
}
5758

5859
bool Parser::MatchPeekToken(Lexer::TokenType t) {
59-
if (auto peek = Current(); peek.Type == t) {
60+
if (auto peek = Peek(); peek.Type == t) {
6061
Advance();
6162
return true;
6263
}
@@ -182,6 +183,10 @@ void Parser::Parser::prefixParse() {
182183
Advance();
183184
}
184185
break;
186+
case Lexer::TokenType::newline:
187+
Advance();
188+
prefixParse();
189+
break;
185190
default:
186191
logger.Log<Log::Level::Error>("Unknown Expression");
187192
}
@@ -379,6 +384,10 @@ void Parser::Parser::parseStatement() {
379384
case In:
380385
parseIn();
381386
break;
387+
case newline:
388+
Advance();
389+
parseStatement();
390+
break;
382391

383392
default:
384393
logger.Log<Log::Level::Warning>("parsing something as an expression statement.\n"

‎primesDemo.basiclang‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
out "Hey! give me a number:\n"
2+
in "{n}"
3+
4+
i = 2
5+
table = 0
6+
7+
res = 0
8+
9+
loop:
10+
mask = 1 << i
11+
if ((table & mask <=> 1)-1) goto increment
12+
13+
res = res + 1
14+
j = i*i
15+
prime_removal:
16+
if ((j <=> (n-1)) - 1) goto increment
17+
18+
table = table | (1 << j)
19+
j = j + i
20+
21+
goto prime_removal
22+
23+
24+
increment:
25+
i = i + 1
26+
if ((i <=> n) + 1) goto loop
27+
28+
out "there are {res} primes below {n}."

‎sumOfNumbersDemo.basiclang‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
out "Hey! Give me a \"number\"! \n"
2+
in "{a}"
3+
4+
i = 0
5+
res = 0
6+
7+
loop:
8+
res = res + i
9+
i = i + 1
10+
if( (a<=>i) + 1 ) goto end
11+
goto loop
12+
end:
13+
14+
out "sum of first {a} numbers is {res}.\n"

0 commit comments

Comments
 (0)