Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpreter. #28441

Draft
wants to merge 11 commits into
base: mainnet
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
"compiler/span",
"docs/grammar",
"errors",
"interpreter",
"leo/package",
"tests/test-framework",
"utils/disassembler",
Expand All @@ -60,6 +61,10 @@ version = "2.3.0"
path = "./errors"
version = "2.3.0"

[workspace.dependencies.leo-interpreter]
path = "./interpreter"
version = "2.3.0"

[workspace.dependencies.leo-package]
path = "./leo/package"
version = "2.3.0"
Expand All @@ -84,6 +89,9 @@ version = "2.3.0"
version = "0.1.24"
default-features = false

[workspace.dependencies.colored]
version = "2.0"

[workspace.dependencies.indexmap]
version = "2.6"
features = [ "serde" ]
Expand All @@ -95,10 +103,15 @@ version = "0.13.0"
version = "0.8"
default-features = false

[workspace.dependencies.rand_chacha]
version = "0.3.0"
default-features = false

[workspace.dependencies.regex]
version = "1.11.1"

[workspace.dependencies.snarkvm]
# path = "../SnarkVM"
version = "1.0.0"

[workspace.dependencies.serde]
Expand Down Expand Up @@ -145,6 +158,9 @@ workspace = true
[dependencies.leo-errors]
workspace = true

[dependencies.leo-interpreter]
workspace = true

[dependencies.leo-package]
workspace = true

Expand All @@ -165,7 +181,7 @@ version = "4.5"
features = [ "derive", "env", "color", "unstable-styles" ]

[dependencies.colored]
version = "2.0"
workspace = true

[dependencies.dotenvy]
version = "0.15.7"
Expand All @@ -177,8 +193,7 @@ workspace = true
workspace = true

[dependencies.rand_chacha]
version = "0.3.0"
default-features = false
workspace = true

[dependencies.self_update]
version = "0.41.0"
Expand Down
24 changes: 23 additions & 1 deletion compiler/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use crate::{Token, tokenizer::*};

use leo_ast::*;
use leo_errors::{Result, emitter::Handler};
use leo_errors::{ParserError, Result, emitter::Handler};
use leo_span::{Span, span::BytePos};

use snarkvm::prelude::Network;
Expand All @@ -49,3 +49,25 @@ pub fn parse<N: Network>(

tokens.parse_program()
}

pub fn parse_expression<N: Network>(handler: &Handler, node_builder: &NodeBuilder, source: &str) -> Result<Expression> {
let mut context = ParserContext::<N>::new(handler, node_builder, crate::tokenize(source, BytePos(0))?);

let expression = context.parse_expression()?;
if context.token.token == Token::Eof {
Ok(expression)
} else {
Err(ParserError::unexpected(context.token.token, Token::Eof, context.token.span).into())
}
}

pub fn parse_statement<N: Network>(handler: &Handler, node_builder: &NodeBuilder, source: &str) -> Result<Statement> {
let mut context = ParserContext::<N>::new(handler, node_builder, crate::tokenize(source, BytePos(0))?);

let statement = context.parse_statement()?;
if context.token.token == Token::Eof {
Ok(statement)
} else {
Err(ParserError::unexpected(context.token.token, Token::Eof, context.token.span).into())
}
}
2 changes: 1 addition & 1 deletion compiler/span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl SourceMap {
}

/// Find the source file containing `pos`.
fn find_source_file(&self, pos: BytePos) -> Option<Rc<SourceFile>> {
pub fn find_source_file(&self, pos: BytePos) -> Option<Rc<SourceFile>> {
Some(self.inner.borrow().source_files[self.find_source_file_index(pos)?].clone())
}

Expand Down
50 changes: 50 additions & 0 deletions errors/src/errors/interpreter_halt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use std::fmt;

use leo_span::Span;

/// Represents the interpreter halting, which should not be considered an
/// actual runtime error.
#[derive(Clone, Debug, Error)]
pub struct InterpreterHalt {
/// Optional Span where the halt occurred.
span: Option<Span>,

/// User visible message.
message: String,
}

impl InterpreterHalt {
pub fn new(message: String) -> Self {
InterpreterHalt { span: None, message }
}

pub fn new_spanned(message: String, span: Span) -> Self {
InterpreterHalt { span: Some(span), message }
}

pub fn span(&self) -> Option<Span> {
self.span
}
}

impl fmt::Display for InterpreterHalt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
7 changes: 7 additions & 0 deletions errors/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub use self::flattener::*;
pub mod loop_unroller;
pub use self::loop_unroller::*;

pub mod interpreter_halt;
pub use self::interpreter_halt::*;

/// Contains the Package error definitions.
pub mod package;
pub use self::package::*;
Expand Down Expand Up @@ -66,6 +69,8 @@ pub enum LeoError {
/// Represents a Compiler Error in a Leo Error.
#[error(transparent)]
CompilerError(#[from] CompilerError),
#[error(transparent)]
InterpreterHalt(#[from] InterpreterHalt),
/// Represents a Package Error in a Leo Error.
#[error(transparent)]
PackageError(#[from] PackageError),
Expand Down Expand Up @@ -110,6 +115,7 @@ impl LeoError {
UtilError(error) => error.error_code(),
LastErrorCode(_) => unreachable!(),
Anyhow(_) => "SnarkVM Error".to_string(), // todo: implement error codes for snarkvm errors.
InterpreterHalt(_) => "Interpreter Halt".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we give these a proper error code?

}
}

Expand All @@ -129,6 +135,7 @@ impl LeoError {
UtilError(error) => error.exit_code(),
LastErrorCode(code) => *code,
Anyhow(_) => 11000, // todo: implement exit codes for snarkvm errors.
InterpreterHalt(_) => 1,
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions interpreter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[package]
name = "leo-interpreter"
version = "2.3.0"
authors = [ "The Leo Team <[email protected]>" ]
description = "Interpreter for the Leo programming language"
homepage = "https://leo-lang.org"
repository = "https://github.com/ProvableHQ/leo"
keywords = [
"aleo",
"cryptography",
"leo",
"programming-language",
"zero-knowledge"
]
categories = [ "compilers", "cryptography", "web-programming" ]
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ]
license = "GPL-3.0"
edition = "2021"
rust-version = "1.82.0"

[dependencies.snarkvm-circuit]
version = "1.0.0"

[dependencies.snarkvm]
workspace = true

[dependencies.leo-ast]
workspace = true

[dependencies.leo-passes]
workspace = true

[dependencies.leo-errors]
workspace = true

[dependencies.leo-parser]
workspace = true

[dependencies.leo-span]
workspace = true

[dependencies.colored]
workspace = true

[dependencies.indexmap]
workspace = true

[dependencies.rand]
workspace = true

[dependencies.rand_chacha]
workspace = true
Loading
Loading