Skip to content

Commit

Permalink
Adds entry attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Oct 8, 2023
1 parent cc7b5ad commit 6efd25d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
19 changes: 17 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"configurations": [
{
"name": "Build",
"name": "Build (std)",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/dist/bin/nitro",
Expand All @@ -16,7 +16,7 @@
"preLaunchTask": "build"
},
{
"name": "Export",
"name": "Export (std)",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/dist/bin/nitro",
Expand All @@ -31,6 +31,21 @@
"RUST_BACKTRACE": "1"
},
"preLaunchTask": "build"
},
{
"name": "Build (cli)",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/dist/bin/nitro",
"args": [
"build",
"${workspaceFolder}/cli"
],
"cwd": "${workspaceFolder}",
"env": {
"RUST_BACKTRACE": "1"
},
"preLaunchTask": "build"
}
],
"version": "2.0.0"
Expand Down
4 changes: 3 additions & 1 deletion cli/App.nt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use nitro.Int32;

class App {
class App;

impl App {
@entry
fn Main(): Int32 {
0
Expand Down
43 changes: 22 additions & 21 deletions stage0/src/ast/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ pub struct Attributes {
condition: Option<(AttributeName, Vec<Expression>)>,
ext: Option<(AttributeName, Extern)>,
repr: Option<(AttributeName, Representation)>,
entry: Option<AttributeName>,
customs: Vec<(AttributeName, Option<Vec<Vec<Expression>>>)>,
}

impl Attributes {
pub fn parse(lex: &mut Lexer, first: AttributeName) -> Result<Self, SyntaxError> {
// Parse the first attribute.
let mut attrs = Self {
public: None,
condition: None,
ext: None,
repr: None,
customs: Vec::new(),
};
let mut attrs = Self::default();

attrs.parse_single(lex, first)?;

Expand All @@ -34,7 +29,7 @@ impl Attributes {
}
None => {
return Err(SyntaxError::new(
lex.last().unwrap().clone(),
lex.last().unwrap(),
"expected an item after this",
));
}
Expand Down Expand Up @@ -62,11 +57,22 @@ impl Attributes {

fn parse_single(&mut self, lex: &mut Lexer, name: AttributeName) -> Result<(), SyntaxError> {
match name.value() {
"entry" => {
// Check for multiple entry.
if self.entry.is_some() {
return Err(SyntaxError::new(
name.span(),
"multiple entry attribute is not allowed",
));
}

self.entry = Some(name);
}
"ext" => {
// Check for multiple ext.
if self.ext.is_some() {
return Err(SyntaxError::new(
name.span().clone(),
name.span(),
"multiple ext attribute is not allowed",
));
}
Expand All @@ -80,15 +86,15 @@ impl Attributes {
name,
match ext.value() {
"C" => Extern::C,
_ => return Err(SyntaxError::new(ext.span().clone(), "unknown extern")),
_ => return Err(SyntaxError::new(ext.span(), "unknown extern")),
},
));
}
"if" => {
// Check for multiple if.
if self.condition.is_some() {
return Err(SyntaxError::new(
name.span().clone(),
name.span(),
"multiple if attribute is not allowed",
));
}
Expand All @@ -102,7 +108,7 @@ impl Attributes {
// Check for multiple pub.
if self.public.is_some() {
return Err(SyntaxError::new(
name.span().clone(),
name.span(),
"multiple pub attribute is not allowed",
));
}
Expand All @@ -124,7 +130,7 @@ impl Attributes {
// Check for multiple repr.
if self.repr.is_some() {
return Err(SyntaxError::new(
name.span().clone(),
name.span(),
"multiple repr attribute is not allowed",
));
}
Expand All @@ -140,18 +146,13 @@ impl Attributes {
"i32" => Representation::I32,
"u8" => Representation::U8,
"un" => Representation::Un,
_ => {
return Err(SyntaxError::new(
repr.span().clone(),
"unknown representation",
));
}
_ => return Err(SyntaxError::new(repr.span(), "unknown representation")),
},
));
}
v if v.chars().next().unwrap().is_ascii_lowercase() => {
return Err(SyntaxError::new(
name.span().clone(),
name.span(),
"an attribute begin with a lower case is a reserved name",
));
}
Expand All @@ -160,7 +161,7 @@ impl Attributes {
match lex.next()? {
Some(Token::OpenParenthesis(_)) => Some(Expression::parse_args(lex)?),
Some(Token::CloseParenthesis(v)) => {
return Err(SyntaxError::new(v.span().clone(), "expect '('"));
return Err(SyntaxError::new(v.span(), "expect '('"));
}
_ => {
lex.undo();
Expand Down
8 changes: 6 additions & 2 deletions stage0/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,13 @@ pub struct SyntaxError {
}

impl SyntaxError {
pub fn new<R: Into<Cow<'static, str>>>(span: Span, reason: R) -> Self {
pub fn new<S, R>(span: S, reason: R) -> Self
where
S: Into<Span>,
R: Into<Cow<'static, str>>,
{
Self {
span,
span: span.into(),
reason: reason.into(),
}
}
Expand Down
6 changes: 6 additions & 0 deletions stage0/src/lexer/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ impl Span {
}
}

impl From<&Self> for Span {
fn from(value: &Self) -> Self {
value.clone()
}
}

impl Add for &Span {
type Output = Span;

Expand Down

0 comments on commit 6efd25d

Please sign in to comment.