Open sdeyoung1974 opened 5 years ago
I have successfully built clox on Windows with Visual Studio 2017 and it's CMake support. You have to add your own CMakeLists.txt file but that is pretty easy. It can look like this to start with.
project(clox)
add_executable(clox
chunk.c
compiler.c
debug.c
main.c
memory.c
object.c
scanner.c
value.c
vm.c
)
Thanks for that info andwi! So I'm a little further along then I was previously. So I created a file called CMakeLists.txt and I added the following.
project(clox)
add_executable(clox chunk.c compiler.c debug.c main.c memory.c object.c scanner.c table.c value.c vm.c )
Everything appears to be working up until this point when I get an error.
CMake Error at CMakeLists.txt:3 (add_executable): Cannot find source file:
chunk.c
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
CMake Error at CMakeLists.txt:3 (add_executable): No SOURCES given to target: clox
Any ideas?
Is CMakeLists.txt in the same directory as the sources?
I found this article usefull when learning about CMake in Visual Studio https://aka.ms/cmake.
Oh no it was in the root of "craftinginterpreters". let me try that..
So I think I got it working. It generated a clox.exe in a debug folder. So it's just an interpreter? I ran the .exe and typed in some of the clox syntax and it responds back. Does Clox use an extension to build source files like in C?
Does Clox use an extension to build source files like in C?
I would think that is a non goal, after all the book is called "Crafting Interpreters".
True! Thanks for your help. I haven't attempted it yet but do you know if the java lox interpreter would be the same process? Or could I use an IDE to build that one?
Little confused about how to do multi line statement. Iām running clox in cmd and it only allows one line at a time. Do I have to type it in-line?
Making a great repl is probably not a goal for the book either. But you can create your lox program in a separate file and pass the path to the file as a command line argument to clox.
As I don't use CMake or Visual Studio, I use a very different method. First of all, I have the Visual Studio variables set with this little function I have in my PowerShell profile:
######################################
# get VS command line tools in powershell
pushd "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools"
cmd /c "VsDevCmd.bat -arch=amd64&set" |
foreach {
if ($_ -match "=") {
$v = $_.split("=")
set-item -force -path "ENV:\$($v[0])" -value "$($v[1])"
}
}
popd
Write-Host "`nVisual Studio 2017 Command Prompt Variables Set." -ForegroundColor Yellow
Then, I have a little script (that's probably a bit overkill) named build.ps1
with the following contents:
if ("bin" -in (Get-ChildItem . -Name)) {
Remove-Item "bin" -Recurse
}
mkdir "bin"
$sources = Get-ChildItem .\src
$cfiles = @()
foreach ($source in $sources) {
if ($source.Extension -eq ".c") {
$cfiles += "..\src\$source"
}
}
pushd "bin"
cl /O2 /Feclox.exe /I src $cfiles
popd
Then I just have all my source files in a src
directory, and it does a clean build each time (computers are fast enough today to do a full build... no need to leave .obj
files lying around to not be updated for reasons and mess up the final executable).
A little late, but I made it work with an ancient TDM version of MINGW and its make tool mingw32-make.
The makefile itself looks along the line of
IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR) -std=c11
ODIR=obj
LIBS=-lm
_DEPS = common.h scanner.h compiler.h vm.h object.h value.h memory.h table.h chunk.h debug.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = memory.o object.o value.o table.o chunk.o debug.o scanner.o compiler.o vm.o main.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
clox.bin: $(OBJ)
gcc -o $@ $^ $(CFLAGS) $(LIBS)
clox.exe: $(OBJ)
gcc -o $@ $^ $(CFLAGS) $(LIBS)
and sits in the src directory.
This is even later, but I'm using Visual Studio 2019 Community, and building clox was "relatively" easy provided you learn a trick that I think isn't very well documented: Visual Studio doesn't have C language project templates per se, you have to use one of the C++ ones and give your C files a .c file extension to tell Visual Studio to use the C compiler instead of the C++ compiler.
1) Create a new project by selecting the template "Empty Project" and give it a name. 2) To create your source files, right click on the project's name in Solution Explorer, and select "Add > New Item..." 3a) Select "Header File (.h)" and type a name. OR 3b) Select "C++ File (.cpp)" BUT NAME IT WITH A .c FILE EXTENSION to make it a C-language file.
The .h and .c files you create appear in the Solution Explorer under the respective "Header Files" or "Source Files" folders.
No separate makefile is needed--any files you add to the project automatically become part of the build. Press F5 to run/debug as usual.
P.S. "relatively easy" compared to the learning curve to get my Java development environment up and running under Visual Studio Code. Oy. In retrospect, I should have chosen a more lightweight IDE geared toward Java beginners. Java is not my primary language.
This is real late, too. A variation of how jptemplin did it in VS 2019 Community Edition is to Create a New Project with just using a C++ Console App. It seemed best to me to create it in the craftinginterpreters/c directory and check "Place solution and project in the same directory" box to keep it clean. Then you can use Project > Add Existing Item (Shift+Alt+A) on all the files.
Since a main.c was included, you can just delete the default .cpp file that a new project automatically creates (with the boiler-plate "Hello World" code), since you don't need it.
HTH.
P.S. I didn't have much luck running anything besides a "Hello World" using Java on VS Code.
Yep, that works too. I used the Empty Project just to avoid the minor annoyance of having to delete the default .cpp file installed by the console project. Sometimes VS Studio does lots of things under the hood, I wanted to make sure there was no C++ detritus silently hanging around in the project, especially if I was writing instructions for others to use.
So much of what I found online suggested "use VS Code for Java" so I did that. Didn't think it would be so hard because I normally live in C#/VS Studio land. My biggest struggle with VS Code for Java was figuring out in which directory to create the project so that the packages directories lined up right. Lots of trial and error (of course no documentation or help on this), especially with trying to get the AstPrinter project in the right place alongside the jlox project. Then there was fussing with the launch.json file. While I can read and understand Java, I don't usually develop in it and I was surprised how many steps it took to set up a working multi-project and multi-source file environment. Nearly all the Java setup tutorials for VS Code online tell you how to compile and run a simple, single-file, single-project "hello world" program. Not very helpful. Where is the Microsoft Quick C (for those as old as me who remember it) or the Turbo Pascal (now, I'm really showing my age) for Java that simplifies all this for casual users/language hackers?
Congratulations on getting the VS Code to work! Really.
(I go back a ways, too, but almost all the time was spent on the IBM i micro-mainframe. Did a little VB and Rexx on a PC, but just started learning C a couple years ago.)
You bring back pleasant memories of programming in Rexx. In the mid-1980's, I was an IBM VM systems programmer on a small 4361 in the business school computing center where I worked at the University of Rochester (NY). Rexx on CMS was my main language. What a great, flexible, language! A few years later, I went to graduate school and that's where I learned C, and a bit later, C++. Most of my professional career was developing C++ applications on Sun Solaris.
This is getting away from the original post, but I have to say that's very interesting. I got a couple paying jobs while I was still in school in 1978. I'm embarrassed to admit that my career was spent coding in RPG and COBOL in small shops. (In my defense, the current version of RPG (which I haven't worked in) is pretty much like a souped-up version of C.) Even as a manager I was able to devote most of my time to coding until the last year I got too busy. I only worked on business apps, but I found it challenging and enjoyable.
I might add that I only had a year-and-a-half of 4-year schooling. I couldn't get my Associates degree from the local community college because of the required Speech Communications class. Sheesh..
Does anyone know the process for the Java lox interpreter? Is it the same process?? Stuck on this part and would appreciate some help on this! Thanks!
For me, this works, Some points are weird but work fine š
Prerequisites
Building
cd tool
pub get
make
I also replaced $(CC)
with gcc
in util\c.make
.
Oh okay, I'll surely try this method and see if it works for me. Thanks for the reply though!
Also, I'd a question regarding the reply - The Dart SDK, GCC, Make should all be path variables right?
@Aditya-Ramachandran IDK about compiling this whole repository and building the book and everything, that's what it sounds like @0xdw is talking about. If you want to simply build the Java JLox interpreter, I have a pretty simple method.
There are 2 PowerShell scripts I use. You do need to use an Admin PowerShell and Set-ExecutionPolicy RemoteSigned
before doing this so scripts will run, but it's a lot easier than installing a half-dozen things.
First, there is this to build:
# build.ps1
# Generate & run Tooling files first
if ("tools" -in (Get-ChildItem . -Name)) {
Remove-Item "tools" -Recurse
}
mkdir "tools"
javac -d .\tools .\src\tool\*.java
java -cp .\tools tool/GenerateAst .\src\lox\
# Finally, generate the class files for realzies
if ("bin" -in (Get-ChildItem . -Name)) {
Remove-Item "bin" -Recurse
}
mkdir "bin"
javac -d .\bin .\src\lox\*.java
Next I use this to run what I've built:
# run.ps1
java -cp .\bin lox/Lox $args[0]
I technically have a third that runs both, but it's not strictly neccisary:
# bnr.ps1
./build
./run $args[0]
You do need to have a recent-ish JVM installed, and in your $PATH. But other than that, you should be able to get the interpreter up and running fairly easily!!
Oh okay, thank you so much @serialhex ... This surely cleared up lots of things!
Oh okay, I'll surely try this method and see if it works for me. Thanks for the reply though!
Also, I'd a question regarding the reply - The Dart SDK, GCC, Make should all be path variables right?
The Dart SDK, GCC, Make should all be path variables right?
Yes, that's right.
Thank you so much!!! @0xdw
For me, this works, Some points are weird but work fine š
Prerequisites
- Dark SDK - https://dart.dev/get-dart
- GCC - https://gcc.gnu.org/install/binaries.html
- Make - http://gnuwin32.sourceforge.net/packages/make.htm
Java
Building
- Clone the repository
- Open the folder in CMD
cd tool
pub get
- Close CMD
- Open the folder in GIT Bash
make
I also replaced
$(CC)
withgcc
inutil\c.make
.
not working. saying make[1]: Entering directory 'C:/home/Github/local-Repo/Interpreter/Lox/craftinginterpreters' gcc c/chunk.c -std=c99 -Wall -Wextra -Werror -Wno-unused-parameter -O3 -flto process_begin: CreateProcess(NULL, gcc -c -std=c99 -Wall -Wextra -Werror -Wno-unused-parameter -O3 -flto -o build/release/clox/chunk.o c/chunk.c, ...) failed. make (e=2): The system cannot find the file specified. make[1]: [util/c.make:42: build/release/clox/chunk.o] Error 2 make[1]: Leaving directory 'C:/home/Github/local-Repo/Interpreter/Lox/craftinginterpreters' make: [Makefile:66: clox] Error 2
please i got this error can u help me
please i got this error can u help me
Hey somehow even if i added the dart sdk path to windows env variable it didnt recognize it and showed me the error. Luckily i had another operating system called ubuntu and it worked fine in that.
Java in Visual Studio Code worked great for me. Setup was easy too. The link to get started is here: https://code.visualstudio.com/docs/languages/java.
When creating your own jlox interpreter just make sure to get the source code's directory structure right. If you clone Bob's repo, File/Open his craftinginterpreters folder in VS Code, and use his directory structure as the example, you're golden.
When you go to "Run and Debug (Ctrl+Shift+D)", you're given the option to "create a launch.json" file. The contents of this JSON-file defines your debug configurations, i.e. which version of main()
along with its args you want to run and debug.
Here's the contents of my launch.json
--hope this helps anyone just getting started:
{
"version": "0.2.0",
"configurations": [
{
"name": "jlox: Launch Current Lox Script",
"request": "launch",
"type": "java",
"mainClass": "com.craftinginterpreters.lox.Lox",
"stopOnEntry": true,
"args": "${file}",
},
{
"name": "jlox: Launch Lox REPL Prompt",
"request": "launch",
"type": "java",
"mainClass": "com.craftinginterpreters.lox.Lox",
"stopOnEntry": true,
},
{
"name": "jlox: Launch AST Printer",
"request": "launch",
"type": "java",
"mainClass": "com.craftinginterpreters.lox.AstPrinter",
"stopOnEntry": true,
},
{
"name": "jlox: Launch Generate AST",
"request": "launch",
"type": "java",
"mainClass": "com.craftinginterpreters.tool.GenerateAst",
"stopOnEntry": true,
"args": "${workspaceFolder}\\Java\\com\\craftinginterpreters\\lox",
},
{
"name": "clox: Launch Current Lox Script",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\c\\bin\\clox.exe",
"args": ["${file}"],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
When creating your own jlox interpreter just make sure to get the source code's directory structure right. If you clone Bob's repo, File/Open his craftinginterpreters folder in VS Code, and use his directory structure as the example, you're golden.
Thanks for this. I wrote my original post over 18 months ago when I wasn't well-versed in VS Code or in the ways of GitHub. Knowing what I know now, I should have (as you wrote) simply cloned his repository instead of what I did which was downloading the code in a .zip and trying to get the directories in the right place. I know better now
Also available for Visual Studio Code users is a fun little syntax highlighting extension for the Lox language. Go to "Extensions (Ctrl+Shift+X)" and Search for "Lox" in the Marketplace. (Thanks, @danman113!) VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=dberezin.lox-language
C/C++ for Visual Studio Code worked great for me too. The link to get started is here: https://code.visualstudio.com/docs/languages/cpp
I added clox to the launch.json
that I posted just above, so you can debug it in VS Code too.
To build it, you'll need a tasks.json
. Here's the contents of mine--I added a "bin" directory to the "c" directory, and chose the "GCC on Windows via MinGW" option:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "clox: Build",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${workspaceFolder}\\c\\*.c",
"-o",
"${workspaceFolder}\\c\\bin\\clox.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: gcc.exe"
}
]
}
For me, this works, Some points are weird but work fine š
Prerequisites
- Dark SDK - https://dart.dev/get-dart
- GCC - https://gcc.gnu.org/install/binaries.html
- Make - http://gnuwin32.sourceforge.net/packages/make.htm
Java
Building
- Clone the repository
- Open the folder in CMD
cd tool
pub get
- Close CMD
- Open the folder in GIT Bash
make
I also replaced
$(CC)
withgcc
inutil\c.make
.
Wow, this worked like a charm. Thank you so much!
I get this any ideas?
I've encountered the issue shown in the screenshot below. Can anyone help me? I've followed the instructions given by others, but I'm still facing the problem.
I've encountered the issue shown in the screenshot below. Can anyone help me? I've followed the instructions given by others, but I'm still facing the problem.
This is just a display error, in fact it is already working. Expr.java and Stmt.java can only be used after the book is compiled. The author once said that adjustments would be made, but it seems he forgot.
Has anyone successfully compiled to Windows? If so, what would be the process? Any help would be appreciated. TIA