Skip to content

Commit

Permalink
Merge branch 'issue-170' into adding-class
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodacus committed Mar 21, 2022
2 parents a73c468 + 3853cf8 commit 1b22846
Show file tree
Hide file tree
Showing 29 changed files with 669 additions and 268 deletions.
6 changes: 6 additions & 0 deletions .github/ISSUE_TEMPLATE/Other.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
name: Other
about: Something else

---

36 changes: 36 additions & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Bug Report
description: Create a report to help us improve
title: "Bug: "
labels: [bug]

body:
- type: markdown
attributes:
value: Thank you for taking the time to file a Bug report.
- type: textarea
attributes:
label: Describe the bug.
description: 'A clear and concise description of what the bug is.'
- type: textarea
attributes:
label: Describe the steps to reproduce the behavior.
description: 'Be clear and concise while describing the steps to ensure that it can be reproduced and potentially fixed'
- type: textarea
attributes:
label: Expected behavior.
description: 'A clear and concise description of what you expected to happen.'
- type: textarea
attributes:
label: Screenshots.
description: 'If applicable, add screenshots to help explain your problem.'
- type: input
attributes:
label: Operating Environment
description: 'What operating system and version are you using? If you are running it on Cloud/Cluster do mention about that.'
validations:
required: true
- type: textarea
attributes:
label: Additional Information
description: 'Any additional information that may help us reproduce the bug.'

29 changes: 29 additions & 0 deletions .github/ISSUE_TEMPLATE/documentation-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Documentation Update
description: Suggest how we can improve our documentation
title: "Documentation: "
labels: [documentation]

body:
- type: markdown
attributes:
value: Thank you for taking the time to file a Documentation update report.
- type: textarea
attributes:
label: What is the current documentation state?
description: 'Copy and paste the text currently in the documentation. If you want to improve graphs or photos, please provide a screenshot of the original.'
- type: textarea
attributes:
label: Where is this stated?
description: 'Provide additional information about the location where you have copied the above statement from.'
- type: textarea
attributes:
label: Why do you want to improve the statement?
description: 'Please explain why the statement needs to be updated. This can be because it is confusing, incorrect, spelling/grammatical errors, etc.'
- type: textarea
attributes:
label: Proposed Statement
description: 'Please propose how you would rewrite the statement. If you have a precise proposal, please make the changes and open a pull request.'
- type: textarea
attributes:
label: Additional context.
description: 'Add any other context or useful information about the documentation update here.'
25 changes: 25 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Feature Request
description: Suggest an idea for this project
title: "Feature Request: "
labels: [enhancement]

body:
- type: markdown
attributes:
value: Thank you for taking the time to file a Feature Request report.
- type: textarea
attributes:
label: Is your feature request related to a problem? Please describe.
description: 'A clear and concise description of what the problem is. Ex. I am always frustrated when [...]'
- type: textarea
attributes:
label: Describe the solution you'd like.
description: 'A clear and concise description of what you want to happen.'
- type: textarea
attributes:
label: Describe alternatives you've considered
description: 'A clear and concise description of any alternative solutions or features you have considered.'
- type: textarea
attributes:
label: Additional context.
description: 'Add any other context or screenshots about the feature request here.'
25 changes: 25 additions & 0 deletions .github/ISSUE_TEMPLATE/user-feedback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: User Feedback
description: Describe how we can improve your experience
title: "User Feedback: "
labels: [user-feedback]

body:
- type: markdown
attributes:
value: Thank you for taking the time to file a User Feedback report.
- type: textarea
attributes:
label: Describe what you are trying to do and why it is not optimal.
description: 'A clear and concise description of what the problem is. Ex. When I reach this point, I cannot [...]'
- type: textarea
attributes:
label: Describe the solution you'd like.
description: 'A clear and concise description of what you want to happen.'
- type: textarea
attributes:
label: Describe alternatives you've considered.
description: 'A clear and concise description of any alternative solutions or features you have considered.'
- type: textarea
attributes:
label: Additional context.
description: 'Add any other context or screenshots about the feature request here.'
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ _Put an `x` in the boxes that apply_

- [ ] These changes have been thoroughly tested.

- [ ] Changes need to be immediately published on npm.
- [ ] Changes need to be immediately published on npm.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ yarn-error.log*

# turbo
.turbo

# history vscode extension
.history/
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,19 @@ bye bhai
```

<h3 align="center">Conditionals</h3>
<p align="center">Bhailang supports simple if else construct , <code>agar bhai</code> block will execute if condition is <code>sahi</code> and <code>warna bhai</code> block will execute if condition is <code>galat</code>.</p>
<p align="center">Bhailang supports if-else-if ladder construct , <code>agar bhai</code> block will execute if condition is <code>sahi</code>, otherwise one of the subsequently added <code>nahi to bhai</code> blocks will execute if their respective condition is <code>sahi</code>, and the <code>warna bhai</code> block will eventually execute if all of the above conditions are <code>galat</code>

```
hi bhai
  bhai ye hai a = 10;
  agar bhai (a < 25) {
   bol bhai "a is less than 25";
  } warna bhai {
   bol bhai "a is greater than or equal to 25";
  }
  agar bhai (a < 20) {
bol bhai "a is less than 20";
} nahi to bhai ( a < 25 ) {
bol bhai "a is less than 25";
} warna bhai {
bol bhai "a is greater than or equal to 25";
}
bye bhai
```

Expand All @@ -147,8 +149,8 @@ hi bhai
bye bhai
```



<h2 align="center">Development</h2>
<p align="center">You can explore abstract syntax tree(AST) of bhailang <a href="https://bhailang-ast.netlify.app/" target="_blank">here</a>.</p>



Expand Down
4 changes: 3 additions & 1 deletion apps/docs/components/Code/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ hi bhai
jab tak bhai (b < 5) {
bol bhai square(b);
agar bhai (b == a){
agar bhai (b == a) {
bol bhai "b is equal to a";
} nahi to bhai (b == 0) {
bol bhai "b is equal to zero";
}
b += 1;
Expand Down
6 changes: 4 additions & 2 deletions apps/docs/components/Documentation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ bye bhai
name: "Conditionals",
description: (
<>
Bhailang supports simple if else construct , <code className="language-cpp">agar bhai</code> block will execute if condition is <code className="language-cpp">sahi</code> and <code className="language-cpp">warna bhai</code> block will execute if condition is <code className="language-cpp">galat</code>.
Bhailang supports if-else-if ladder construct , <code className="language-cpp">agar bhai</code> block will execute if condition is <code className="language-cpp">sahi</code>, otherwise one of the subsequently added <code className="language-cpp">nahi to bhai</code> blocks will execute if their respective condition is <code className="language-cpp">sahi</code>, and the <code className="language-cpp">warna bhai</code> block will eventually execute if all of the above conditions are <code className="language-cpp">galat</code>.
</>
),
code: `hi bhai
bhai ye hai a = 10;
agar bhai (a < 25) {
agar bhai (a < 20) {
bol bhai "a is less than 20";
} nahi to bhai ( a < 25 ) {
bol bhai "a is less than 25";
} warna bhai {
bol bhai "a is greater than or equal to 25";
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/components/common/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const bhaiLangSyntax = languages.extend("clike", {
pattern: /(["'])((?:\\\1|(?:(?!\1)).)*)(\1)/,
greedy: true,
},
keyword: /\b(?:hi bhai|bye bhai|bol bhai|bhai ye hai|nalla|agar bhai|warna bhai|jab tak bhai|bas kar bhai|agla dekh bhai|apna funda|rakh le bhai|samajh le bhai|jo ki ek| jo ki ek bhai|hai bhai|janam|iska)\b/,
keyword: /\b(?:hi bhai|bye bhai|bol bhai|bhai ye hai|nalla|agar bhai|nahi to bhai|warna bhai|jab tak bhai|bas kar bhai|agla dekh bhai|apna funda|rakh le bhai|samajh le bhai|jo ki ek| jo ki ek bhai|hai bhai|janam|iska)\b/,
boolean: /\b(?:sahi|galat)\b/,
number: /(?:(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[-+]?\d+)?)i?/i,
operator:
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bhailang",
"version": "0.0.9",
"version": "0.1.0",
"description": "CLI for bhai-lang",
"source": "src/index.ts",
"main": "bin/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/interpreter/src/components/dataClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export function sanatizeData(data:any|unknown):DataObject{
if(typeof data=='function'){
return new CallableObject(data);
}
if(typeof data=='object' && data.isDataObject){
if(data.isDataObject==true){
return data as DataObject;
}
else throw new Error(`Ye kya kar raha hai: "${data}" sahi nhi hai. ye konsa data type hai bhai`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export default class BinaryExpression implements Visitor {
);
}

let left:DataObject=new NullObject(), right:DataObject=new NullObject();

// handling logical & binary both at the same place as both operate on two operands
if (node.type == NodeType.BinaryExpression) {
if (node.operator !== "==" && node.operator !== "!="&&node.operator !==".") {
Expand All @@ -27,14 +25,13 @@ export default class BinaryExpression implements Visitor {
}
else if (node.operator === ".") {
return this.performDotOperation(node);

}
} else if (node.type == NodeType.LogicalExpression) {
this._checkNalla(node);
}

left = sanatizeData(InterpreterModule.getVisitor(node.left.type).visitNode(node.left));
right = sanatizeData(InterpreterModule.getVisitor(node.right.type).visitNode(node.right));
const left = sanatizeData(InterpreterModule.getVisitor(node.left.type).visitNode(node.left));
const right = sanatizeData(InterpreterModule.getVisitor(node.right.type).visitNode(node.right));

return getOperationValue({ left, right }, node.operator);
}
Expand Down
53 changes: 37 additions & 16 deletions packages/interpreter/src/components/visitor/ifStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,53 @@ import { ASTNode } from "bhai-lang-parser";

import InterpreterModule from "../../module/interpreterModule";
import Scope from "../scope";
import { DataTypes, sanatizeData } from "../dataClass";
import { sanatizeData } from "../dataClass";


export default class IfStatement implements Visitor {

private evaluateNode(node: ASTNode | undefined, parentScope: Scope) {
if (node) {
InterpreterModule.setCurrentScope(new Scope(parentScope));
InterpreterModule.getCurrentScope().setLoop(parentScope.isLoop());
InterpreterModule.getVisitor(node.type).visitNode(node);
}
}

visitNode(node: ASTNode) {
const test = node.test;
const parentScope = InterpreterModule.getCurrentScope();
if (test) {
const testResult = sanatizeData(InterpreterModule.getVisitor(test.type).visitNode(test));
if (testResult.getType() !== DataTypes.Null && testResult.getValue() === true) {
const consequent = node.consequent;
if (consequent) {
InterpreterModule.setCurrentScope(new Scope(parentScope));
InterpreterModule.getCurrentScope().setLoop(parentScope.isLoop());
InterpreterModule.getVisitor(consequent.type).visitNode(consequent);
}
} else {
const alternate = node.alternate;
if (alternate) {
InterpreterModule.setCurrentScope(new Scope(parentScope));
InterpreterModule.getCurrentScope().setLoop(parentScope.isLoop());
InterpreterModule.getVisitor(alternate.type).visitNode(alternate);
if (testResult.getValue()) {
this.evaluateNode(node.consequent, parentScope);
}
else {
const alternates = node.alternates;
if (alternates && alternates.length > 0) {
for (var alternate of alternates) {
const alternateTest = alternate.test;
if (!alternateTest) {
// Reached the "warna bhai" node in the alternate list, simply evaluate it and break
this.evaluateNode(alternate, parentScope);
break;
} else {
// Evaluate the "test" condition of the "nahi to bhai" node
// If the condition is true, evaluate the node and break
const testResult = sanatizeData(InterpreterModule.getVisitor(alternateTest!.type).visitNode(alternateTest));
if (testResult.getValue()) {
this.evaluateNode(alternate, parentScope);
break;
}
}
if (testResult.getValue()) {
this.evaluateNode(alternate.consequent, parentScope);
break;
}
}
}
}
}

}
parentScope.setBreakStatement(InterpreterModule.getCurrentScope().isBreakStatement());
parentScope.setContinueStatement(InterpreterModule.getCurrentScope().isContinueStatement());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Visitor from ".";
import { ASTNode, NodeType } from "bhai-lang-parser";
import { ASTNode } from "bhai-lang-parser";

import InvalidStateException from "../../exceptions/invalidStateException";
import InterpreterModule from "../../module/interpreterModule";
import { BooleanObject, NullObject, sanatizeData } from "../dataClass";
import { sanatizeData } from "../dataClass";

export default class VariableDeclaration implements Visitor {
visitNode(node: ASTNode) {
Expand All @@ -13,13 +13,7 @@ export default class VariableDeclaration implements Visitor {

const identifier = node.id.name;

let value;

if (node.init.type === NodeType.NullLiteral) value = new NullObject();
else if (node.init.type === NodeType.BooleanLiteral)
value = new BooleanObject(node.init.value === "sahi" ? true : false);
else
value = sanatizeData(InterpreterModule.getVisitor(node.init.type).visitNode(node.init));
let value= sanatizeData(InterpreterModule.getVisitor(node.init.type).visitNode(node.init));

const currentScope = InterpreterModule.getCurrentScope();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class WhileStatement implements Visitor {


for (let testResult = getConditionValue(), executions = 0;
testResult.getValue() === true;
testResult.getValue();
testResult = getConditionValue(), executions++) {

if (InterpreterModule.getCurrentScope().isBreakStatement()) {
Expand Down
21 changes: 13 additions & 8 deletions packages/interpreter/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,21 @@ export function getOperationValue(
throw exception;

case "&&":
// if (operands.left.getType() !== DataTypes.Boolean || operands.right.getType() !== DataTypes.Boolean) {
// throw exception;
// }
return new BooleanObject(operands.left.getValue() && operands.right.getValue());
if(operands.left.getValue()){
return operands.right;
}
else {
return operands.left;
}

case "||":
return new BooleanObject((operands.left.getValue() || operands.right.getValue())==true);

case ".":

if(operands.left.getValue()){
return operands.left;
}
else{
return operands.right;
}

default:
throw new InvalidStateException(`Unsupported operator: ${operator}`);
}
Expand Down

0 comments on commit 1b22846

Please sign in to comment.