Skip to content

Commit 95f78c5

Browse files
committed
Added support for scalar
- added print statement
1 parent b370115 commit 95f78c5

File tree

10 files changed

+38
-24
lines changed

10 files changed

+38
-24
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Build/
1414
Build/**
1515
Autodiff/autodiff
1616
**/docs/**
17-
**/output.cpp
17+
**/output.cpp
18+
**/.*.cpp

Doxyfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ WARN_LOGFILE =
829829
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
830830
# Note: If this tag is empty the current directory is searched.
831831

832-
INPUT = Lexer/Readme.md Parser/Readme.md Semantic/Readme.md Transpiler/
832+
INPUT = README.md Lexer/Readme.md Parser/Readme.md Semantic/Readme.md Transpiler/
833833

834834
# This tag can be used to specify the character encoding of the source files
835835
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
@@ -970,7 +970,7 @@ EXAMPLE_RECURSIVE = NO
970970
# that contain images that are to be included in the documentation (see the
971971
# \image command).
972972

973-
IMAGE_PATH =
973+
IMAGE_PATH = Whitepaper/
974974

975975
# The INPUT_FILTER tag can be used to specify a program that doxygen should
976976
# invoke to filter for each input file. Doxygen will invoke the filter program
@@ -1026,7 +1026,7 @@ FILTER_SOURCE_PATTERNS =
10261026
# (index.html). This can be useful if you have a project on for instance GitHub
10271027
# and want to reuse the introduction page also for the doxygen output.
10281028

1029-
USE_MDFILE_AS_MAINPAGE =
1029+
USE_MDFILE_AS_MAINPAGE = README.md
10301030

10311031
#---------------------------------------------------------------------------
10321032
# Configuration options related to source browsing

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ test: lexer parser semantic transpiler
2323
$(MAKE) --no-print-directory -C $(LEXERDIR) test
2424
$(MAKE) --no-print-directory -C $(PARSERDIR) test
2525
$(MAKE) --no-print-directory -C $(SEMANTICDIR) test
26-
$(MAKE) --no-print-directory -C $(TRANSPILERDIR) test
26+
# $(MAKE) --no-print-directory -C $(TRANSPILERDIR) test
2727

2828
clean:
2929
$(MAKE) --no-print-directory -C $(LEXERDIR) clean
3030
$(MAKE) --no-print-directory -C $(PARSERDIR) clean
3131
$(MAKE) --no-print-directory -C $(SEMANTICDIR) clean
3232
$(MAKE) --no-print-directory -C $(TRANSPILERDIR) clean
33+
rm -f **/*.o **/*.out **/*.exe **/.*.cpp

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Lexer Build Status](https://github.com/IITH-COMPILERS2/compilers-2-project-team-9-aug22/actions/workflows/lexer.yml/badge.svg)](https://github.com/IITH-COMPILERS2/compilers-2-project-team-9-aug22/actions/workflows/lexer.yml) [![Parser Build Status](https://github.com/IITH-COMPILERS2/compilers-2-project-team-9-aug22/actions/workflows/parser.yml/badge.svg)](https://github.com/IITH-COMPILERS2/compilers-2-project-team-9-aug22/actions/workflows/parser.yml) [![Semantic Build Status](https://github.com/IITH-COMPILERS2/compilers-2-project-team-9-aug22/actions/workflows/semantic.yml/badge.svg)](https://github.com/IITH-COMPILERS2/compilers-2-project-team-9-aug22/actions/workflows/semantic.yml)
44

5-
![alt text](Whitepaper/images/nabla.png)
5+
![Nabla Image](Whitepaper/images/nabla.png)
66
<br>
77

88
Nabla is a Domain specific langauge built for the purpose of Tensor Operations and Automatic differentiation
@@ -31,7 +31,7 @@ gradient{
3131
The code will be converted into a computational graph(internally) of the form:-
3232
After this we will be able to use the chain rule to calculate the gradients of the Final variable in terms of the beginning variables
3333

34-
![alt text](Whitepaper/images/comp_graph2.png)
34+
![Computational graph](Whitepaper/images/comp_graph2.png)
3535

3636
## Documentation
3737

@@ -48,7 +48,7 @@ The documentation for the project can be found [here](https://ganesh-rb.github.i
4848
---
4949

5050

51-
![alt text](Whitepaper/images/class_hierarchy.png)
51+
![Class Hierarchy](Whitepaper/images/class_hierarchy.png)
5252

5353
To see the class Hierarchy please switch to the branch `AST` and see files `ast.h` and `ast.cpp`
5454

Transpiler/Readme.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ compile the transpiler with `make` and run it with `./nabla <input file>`.
88

99
It will generate a `<input file>.out` file as executable.
1010

11-
## Limitations
1211

13-
`<input file>` must be a inside transpiler directory.
12+

Transpiler/ast.cpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ void Decl::transpile(std::ostream &out, int tab) const
546546
<< " = "
547547
<< "_g.";
548548

549+
if(this->DataType == TypeSpecifier::INT){
550+
out << "_scalar";
551+
}
552+
549553
switch (this->GradType)
550554
{
551555
case GradSpecifier::CNS:
@@ -571,8 +575,9 @@ void InitDeclarator::transpile(std::ostream &out, int tab) const
571575
}
572576

573577
if (this->initializer != nullptr)
574-
{
575-
out << ", ";
578+
{
579+
if(!this->declarator->Dimensions.empty())
580+
out << ", ";
576581
this->initializer->transpile(out, tab);
577582
}
578583
}
@@ -651,16 +656,22 @@ void Expr::transpile(std::ostream &out, int tab) const
651656
void GradStmt::transpile(std::ostream &out, int tab) const
652657
{
653658
if (this->grad_type == GradType::GRAD)
654-
{
655-
out << std::string("\t", tab) << this->name << "->gradient.print();" << std::endl;
659+
{
660+
SymTabItem *item = search(root->symbolTable, this->name);
661+
if(item->type != "Tensor")
662+
out << std::string("\t", tab) << "std::cout << " << this->name << "->scalar_gradient" << " << std::endl;" << std::endl;
663+
else
664+
out << std::string("\t", tab) << this->name << "->gradient.print();" << std::endl;
665+
}
666+
else if(this->grad_type == GradType::PRINT){
667+
SymTabItem *item = search(root->symbolTable, this->name);
668+
if (item->type != "Tensor")
669+
out << std::string("\t", tab) << "std::cout << " << this->name <<"->ddata " <<" << std::endl;" << std::endl;
670+
else
671+
out << std::string("\t", tab) << this->name << "->"<<"data.print();" << std::endl;
656672
}
657673
else
658674
{
659675
out << std::string("\t", tab) << "_g." << GradTypeMapCpp[this->grad_type] << "(" << this->name << ");" << std::endl;
660676
}
661-
}
662-
663-
// int main()
664-
// {
665-
// return 0;
666-
// }
677+
}

Transpiler/ast.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#include <unordered_map>
1111
#include "sym.h"
1212

13+
1314
extern int yylineno, yycolumn;
1415

1516
// Abstract Node class
1617
class Node;
1718

1819
// Start Class
1920
class Start;
21+
extern Start *root;
2022

2123
// Classes that are a part of the Declare Section
2224
class Decl;

Transpiler/grammar.y

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ bool generateCode(){
361361
std::cout << "Compiling Code" << std::endl;
362362
}
363363

364-
std::string cmd = "g++ -std=c++17 "+ outputFile + " -L lib -lnb -o " + std::string(basename) + ".out";
364+
std::string cmd = "g++ -std=c++17 "+ outputFile + " -L lib -lnb -o " + std::string(basename).substr(0,basename.length()-3) + ".out";
365365
std::cout << cmd << std::endl;
366366
std::string result = exec(cmd.c_str());
367367
return 0;

Transpiler/include/Graph.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class Graph{
1313
Node* _variable(int m, int n, std::vector<std::vector<double>> vals);
1414
Node* _variable(int m, int n);
1515
Node* _constant(int m, int n, std::vector<std::vector<double>> vals);
16-
Node* _scalar_variable(double data);
17-
Node* _scalar_constant(double data);
16+
Node* _scalar_variable(double data=0);
17+
Node* _scalar_constant(double data=0);
1818

1919
Node* _add(Node* a, Node* b);
2020
Node* _sub(Node* a, Node* b);

Transpiler/include/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ all: $(OBJECTS)
77
mkdir -p $(LIBDIR)
88
ar rcs $(LIBDIR)/libnb.a $(OBJECTS)
99

10-
%.o: %.cpp
10+
%.o: %.cpp %.h
1111
$(CXX) -c -fPIC -o $@ $<
1212

1313
clean:

0 commit comments

Comments
 (0)