Skip to content

Commit

Permalink
add import
Browse files Browse the repository at this point in the history
  • Loading branch information
lbmonsalve committed Sep 15, 2024
1 parent 42263f1 commit dc756fd
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 96 deletions.
3 changes: 3 additions & 0 deletions Examples/import/a.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print "a.lox";

import "b";
3 changes: 3 additions & 0 deletions Examples/import/b.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "a";

print "b.lox";
6 changes: 6 additions & 0 deletions Lox/Ast/IStmtVisitor.rbbas
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ Protected Interface IStmtVisitor
End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitImport(stmt As Lox.Ast.Import) As Variant

End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitModuleStmt(stmt As Lox.Ast.ModuleStmt) As Variant

Expand Down
58 changes: 58 additions & 0 deletions Lox/Ast/Import.rbbas
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#tag Class
Protected Class Import
Inherits Lox.Ast.Stmt
#tag Method, Flags = &h0
Function Accept(visitor As IStmtVisitor) As Variant
Return visitor.VisitImport(Self)
End Function
#tag EndMethod

#tag Method, Flags = &h1000
Sub Constructor(name As Token)
Self.Name= name
End Sub
#tag EndMethod


#tag Property, Flags = &h0
Name As Token
#tag EndProperty


#tag ViewBehavior
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
InheritedFrom="Object"
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass
32 changes: 31 additions & 1 deletion Lox/Inter/Interpreter.rbbas
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Implements Lox.Ast.IExprVisitor,Lox.Ast.IStmtVisitor
Catch exp As StackOverflowException
HadRuntimeError= True
#pragma BreakOnExceptions Off
Raise New RuntimeError(New Token(Lox.TokenType.EOF, "",Nil, -1), _
Raise New RuntimeError(New Token(Lox.TokenType.NIL_, "",Nil, -1), _
"StackOverflowException")
Finally
mEnvironment= previous
Expand Down Expand Up @@ -161,6 +161,8 @@ Implements Lox.Ast.IExprVisitor,Lox.Ast.IStmtVisitor
mEnvironment= mGlobals

mLocals= New Lox.Misc.CSDictionary

mImportFiles= New Dictionary
End Sub
#tag EndMethod

Expand Down Expand Up @@ -764,6 +766,30 @@ Implements Lox.Ast.IExprVisitor,Lox.Ast.IStmtVisitor
End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitImport(stmt As Lox.Ast.Import) As Variant
Dim file As FolderItem= Findfile(stmt.Name.Literal.StringValue)
If file Is Nil Or Not file.Exists Then
HadRuntimeError= True
#pragma BreakOnExceptions Off
Raise New RuntimeError(stmt.Name, "Couldn't find a module on "+ _
file.AbsoluteNativePathLox+ ".")
End If

If mImportFiles.HasKey(file.AbsoluteNativePathLox) Then Return Nil

Dim statements() As Lox.Ast.Stmt= ResolveFile(file, stmt.Name, Self)

mImportFiles.Value(file.AbsoluteNativePathLox)= True

For Each statement As Lox.Ast.Stmt In statements
execute statement
Next

Return Nil
End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitInterpolatedStr(expr As Lox.Ast.InterpolatedStr) As Variant
Dim sb() As String
Expand Down Expand Up @@ -1035,6 +1061,10 @@ Implements Lox.Ast.IExprVisitor,Lox.Ast.IStmtVisitor
Private mGlobals As Environment
#tag EndProperty

#tag Property, Flags = &h21
Private mImportFiles As Dictionary
#tag EndProperty

#tag Property, Flags = &h21
Private mLocals As Lox.Misc.CSDictionary
#tag EndProperty
Expand Down
6 changes: 6 additions & 0 deletions Lox/Inter/Resolver.rbbas
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ Implements Lox.Ast.IExprVisitor,Lox.Ast.IStmtVisitor
End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitImport(stmt As Lox.Ast.Import) As Variant

End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitInterpolatedStr(expr As Lox.Ast.InterpolatedStr) As Variant
For Each part As Lox.Ast.Expr In expr.Parts
Expand Down
101 changes: 96 additions & 5 deletions Lox/Lox.rbbas
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
#tag Module
Protected Module Lox
#tag Method, Flags = &h0
Function AbsoluteNativePathLox(Extends obj As FolderItem) As String
#if RBVersion< 2013
Return obj.AbsolutePath
#else
Return obj.NativePath
#endif
End Function
#tag EndMethod

#tag Method, Flags = &h0
Sub AddSearchPath(path As String)
Dim folder As FolderItem

Try
#pragma BreakOnExceptions Off
folder= New FolderItem(path)
Catch exc
Return
End Try

If Not folder.Directory Then Return

ChkSearchPaths

mSearchPaths.Append folder
End Sub
#tag EndMethod

#tag Method, Flags = &h21
Private Function AreEqual(expected As Double, actual As Double, tolerance As Double = 0.000001) As Boolean
Dim diff As Double= Abs(expected- actual)
Expand All @@ -24,6 +53,14 @@ Protected Module Lox
End Sub
#tag EndMethod

#tag Method, Flags = &h21
Private Sub ChkSearchPaths()
If mSearchPaths.Ubound= -1 Then
mSearchPaths.Append app.ExecutableFile.Parent
End If
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Function Count(Extends obj() As Token) As Integer
Return obj.Ubound+ 1
Expand Down Expand Up @@ -72,6 +109,24 @@ Protected Module Lox
End Function
#tag EndMethod

#tag Method, Flags = &h21
Private Function FindFile(name As String) As FolderItem
ChkSearchPaths

If name.InStr(".lox")= 0 Then name= name+ ".lox"

Dim file As New FolderItem(name) // full path
If Not (file Is Nil) And file.Exists Then Return file

For Each path As FolderItem In mSearchPaths // search paths
file= path.Child(name)
If Not (file Is Nil) And file.Exists Then Return file
Next

Return file
End Function
#tag EndMethod

#tag Method, Flags = &h21
Private Function IIf(stmt As Boolean, ifTrue As TokenType, ifFalse As TokenType) As TokenType
If stmt Then Return ifTrue Else Return ifFalse
Expand Down Expand Up @@ -139,6 +194,38 @@ Protected Module Lox
End Sub
#tag EndMethod

#tag Method, Flags = &h21
Private Function ResolveFile(file As FolderItem, tok As Token, interp As Lox.Inter.Interpreter) As Lox.Ast.Stmt()
Dim ti As TextInputStream= TextInputStream.Open(file)

Dim scanner As New Lox.Scanner(ti.ReadAll)
Dim tokens() As Lox.Token= scanner.Scan
If scanner.HadError Then
interp.HadRuntimeError= True
#pragma BreakOnExceptions Off
Raise New RuntimeError(tok, "scanner.HadError.")
End If

Dim parser As New Lox.Parser(tokens)
Dim statements() As Lox.Ast.Stmt= parser.Parse
If parser.HadError Then
interp.HadRuntimeError= True
#pragma BreakOnExceptions Off
Raise New RuntimeError(tok, "parser.HadError.")
End If

Dim resolver As New Lox.Inter.Resolver(Lox.Interpreter)
resolver.Resolve(statements)
If resolver.HadError Then
interp.HadRuntimeError= True
#pragma BreakOnExceptions Off
Raise New RuntimeError(tok, "resolver.HadError.")
End If

Return statements
End Function
#tag EndMethod

#tag Method, Flags = &h1
Protected Sub RuntimeError(error As RuntimeError)
Dim msg As String= error.Message+ EndOfLine+ "[line "+ _
Expand Down Expand Up @@ -274,8 +361,6 @@ Protected Module Lox
Return "ELVIS"
Case TokenType.ELVIS_DOT
Return "ELVIS_DOT"
Case TokenType.USING_
Return "USING"
Case TokenType.LEFT_BRACKET
Return "LEFT_BRACKET"
Case TokenType.RIGHT_BRACKET
Expand All @@ -286,6 +371,8 @@ Protected Module Lox
Return "HASHTAG_BRACE"
Case TokenType.STRING_INTERPOLATION
Return "STRING_INTERPOLATION"
Case TokenType.IMPORT
Return "IMPORT"

Case Else
Return "STRINGIFY->"
Expand Down Expand Up @@ -434,6 +521,10 @@ Protected Module Lox
Private mPrintOut As Writeable
#tag EndProperty

#tag Property, Flags = &h21
Private mSearchPaths() As FolderItem
#tag EndProperty

#tag ComputedProperty, Flags = &h0
#tag Getter
Get
Expand All @@ -453,7 +544,7 @@ Protected Module Lox
#tag EndComputedProperty


#tag Constant, Name = Version, Type = String, Dynamic = False, Default = \"0.0.240913", Scope = Public
#tag Constant, Name = Version, Type = String, Dynamic = False, Default = \"0.0.240915", Scope = Public
#tag EndConstant


Expand Down Expand Up @@ -514,12 +605,12 @@ Protected Module Lox
GREATER_GREATER
ELVIS
ELVIS_DOT
USING_
LEFT_BRACKET
RIGHT_BRACKET
FAT_ARROW
HASHTAG_BRACE
STRING_INTERPOLATION
STRING_INTERPOLATION
IMPORT
#tag EndEnum


Expand Down
18 changes: 17 additions & 1 deletion Lox/Misc/AstPrinter.rbbas
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,25 @@ Implements Lox.Ast.IExprVisitor,Lox.Ast.IStmtVisitor
End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitImport(stmt As Lox.Ast.Import) As Variant
Return Parenthesize2("import", stmt.Name)
End Function
#tag EndMethod

#tag Method, Flags = &h0
Function VisitInterpolatedStr(expr As Lox.Ast.InterpolatedStr) As Variant
Dim sb() As String

sb.Append "(interpolated str "

For Each part As Lox.Ast.Expr In expr.Parts
sb.Append part.Accept(Self)
Next

sb.Append ")"

Return Join(sb, "")
End Function
#tag EndMethod

Expand Down Expand Up @@ -324,7 +340,7 @@ Implements Lox.Ast.IExprVisitor,Lox.Ast.IStmtVisitor

#tag Method, Flags = &h0
Function VisitPostfix(expr As Lox.Ast.Postfix) As Variant

Return Parenthesize2("postfix", expr.Name, expr.Operator, expr.Left)
End Function
#tag EndMethod

Expand Down
Loading

0 comments on commit dc756fd

Please sign in to comment.